(empty log message)
@@ -1,164 +0,0 @@ | ||
1 | -/*! | |
2 | - WorkspaceHelper.h | |
3 | - | |
4 | - Workspace インタフェース・ラッパクラスの定義 | |
5 | -*/ | |
6 | -#pragma once | |
7 | -#include "types/WorkspaceTypes.h" | |
8 | -#include "UTF8StringArray.h" | |
9 | - | |
10 | - | |
11 | -//////////////////////////////////////////////////////////////////////////////////////////////////////// | |
12 | -// インタフェース・ラッパ | |
13 | -//////////////////////////////////////////////////////////////////////////////////////////////////////// | |
14 | -/*! | |
15 | - 文字列ラッパクラス | |
16 | -*/ | |
17 | -class CIStringWrap : public IString | |
18 | -{ | |
19 | -public: | |
20 | - ///////////////////////////////////////////////////////////////// | |
21 | - // コンストラクタ | |
22 | - ///////////////////////////////////////////////////////////////// | |
23 | - /*! | |
24 | - コンストラクタ | |
25 | - */ | |
26 | - CIStringWrap() : m_str(m_buffer) | |
27 | - { | |
28 | - } | |
29 | - | |
30 | - /*! | |
31 | - コンストラクタ | |
32 | - */ | |
33 | - CIStringWrap(CUTF8String &str) : m_str(str) | |
34 | - { | |
35 | - } | |
36 | - | |
37 | - /*! | |
38 | - コンストラクタ | |
39 | - */ | |
40 | - CIStringWrap(LPCU8STR str) : m_str(m_buffer) | |
41 | - { | |
42 | - m_buffer = str; | |
43 | - } | |
44 | - | |
45 | - ///////////////////////////////////////////////////////////////// | |
46 | - // 操作 | |
47 | - ///////////////////////////////////////////////////////////////// | |
48 | - /*! | |
49 | - 文字列を設定 | |
50 | - */ | |
51 | - virtual void SetString(LPCU8STR str, int length) | |
52 | - { | |
53 | - m_str.SetString(str, length); | |
54 | - } | |
55 | - | |
56 | - /*! | |
57 | - 取得 | |
58 | - */ | |
59 | - virtual LPCU8STR GetString() const | |
60 | - { | |
61 | - return m_str.GetString(); | |
62 | - } | |
63 | - | |
64 | - /*! | |
65 | - 長さ取得 | |
66 | - */ | |
67 | - virtual int GetLength() const | |
68 | - { | |
69 | - return m_str.GetLength(); | |
70 | - } | |
71 | - | |
72 | - /*! | |
73 | - 空? | |
74 | - */ | |
75 | - int IsEmpty() | |
76 | - { | |
77 | - return m_str.IsEmpty(); | |
78 | - } | |
79 | - | |
80 | - /*! | |
81 | - 取得 | |
82 | - */ | |
83 | - operator CUTF8String() | |
84 | - { | |
85 | - return m_str; | |
86 | - } | |
87 | - | |
88 | - /*! | |
89 | - 設定 | |
90 | - */ | |
91 | - void Attach(CUTF8String &str) | |
92 | - { | |
93 | - m_str = str; | |
94 | - } | |
95 | - | |
96 | -protected: | |
97 | - ///////////////////////////////////////////////////////////////// | |
98 | - // メンバ変数 | |
99 | - ///////////////////////////////////////////////////////////////// | |
100 | - //! 参照 | |
101 | - CUTF8String &m_str; | |
102 | - | |
103 | - //! 内部バッファ | |
104 | - CUTF8String m_buffer; | |
105 | -}; | |
106 | - | |
107 | - | |
108 | -/*! | |
109 | - 文字列配列ラッパクラス | |
110 | -*/ | |
111 | -class CIStringArrayWrap : public IStringArray | |
112 | -{ | |
113 | -public: | |
114 | - CIStringArrayWrap(CUTF8StringArray &arr) : | |
115 | - m_array(arr) | |
116 | - { | |
117 | - }; | |
118 | - | |
119 | - //! 初期化 | |
120 | - virtual void Clear() | |
121 | - { | |
122 | - m_array.RemoveAll(); | |
123 | - }; | |
124 | - | |
125 | - //! サイズ設定 | |
126 | - virtual void SetSize(int size) | |
127 | - { | |
128 | - m_array.SetSize(size); | |
129 | - }; | |
130 | - | |
131 | - //! サイズ取得 | |
132 | - virtual int GetSize() | |
133 | - { | |
134 | - return m_array.GetCount(); | |
135 | - }; | |
136 | - | |
137 | - //! 保持している文字列を取得 | |
138 | - virtual void AddString(LPCU8STR str) | |
139 | - { | |
140 | - m_array.Add(CUTF8String(str)); | |
141 | - }; | |
142 | - | |
143 | - //! 保持している文字列を取得 | |
144 | - virtual LPCU8STR GetString(int index) | |
145 | - { | |
146 | - return m_array[index]; | |
147 | - }; | |
148 | - | |
149 | - //! 保持している文字列を取得 | |
150 | - virtual void SetString(int index, LPCU8STR str) | |
151 | - { | |
152 | - m_array[index] = str; | |
153 | - }; | |
154 | - | |
155 | - ///////////////////////////////////////////////////////////////////// | |
156 | - // 補助 | |
157 | - ///////////////////////////////////////////////////////////////////// | |
158 | - //! CUTF8StringArray取得 | |
159 | - CUTF8StringArray &GetStringArray() { return m_array; }; | |
160 | - | |
161 | -protected: | |
162 | - //! 保持中の配列 | |
163 | - CUTF8StringArray &m_array; | |
164 | -}; |
@@ -8,8 +8,9 @@ | ||
8 | 8 | */ |
9 | 9 | |
10 | 10 | #pragma once |
11 | +#include "types/WorkspaceTypes.h" | |
12 | +#include "IStringHelper.h" | |
11 | 13 | #include "WorkspaceAccess.h" |
12 | -#include "WorkspaceHelper.h" | |
13 | 14 | #include "SyncObject.h" |
14 | 15 | #include "InterfaceAutoPtr.h" |
15 | 16 |
@@ -1 +1 @@ | ||
1 | -111 | |
1 | +ローレベルインタフェース定義ファイル用ディレクトリ |
@@ -0,0 +1,165 @@ | ||
1 | +/*! | |
2 | + IStringHelper.h | |
3 | + | |
4 | + IString/IStringArray インタフェース・ラッパクラスの定義 | |
5 | +*/ | |
6 | +#pragma once | |
7 | +#include "types/StringTypes.h" | |
8 | +#include "UTF8String.h" | |
9 | +#include "UTF8StringArray.h" | |
10 | + | |
11 | + | |
12 | +//////////////////////////////////////////////////////////////////////////////////////////////////////// | |
13 | +// インタフェース・ラッパ | |
14 | +//////////////////////////////////////////////////////////////////////////////////////////////////////// | |
15 | +/*! | |
16 | + 文字列ラッパクラス | |
17 | +*/ | |
18 | +class CIStringWrap : public IString | |
19 | +{ | |
20 | +public: | |
21 | + ///////////////////////////////////////////////////////////////// | |
22 | + // コンストラクタ | |
23 | + ///////////////////////////////////////////////////////////////// | |
24 | + /*! | |
25 | + コンストラクタ | |
26 | + */ | |
27 | + CIStringWrap() : m_str(m_buffer) | |
28 | + { | |
29 | + } | |
30 | + | |
31 | + /*! | |
32 | + コンストラクタ | |
33 | + */ | |
34 | + CIStringWrap(CUTF8String &str) : m_str(str) | |
35 | + { | |
36 | + } | |
37 | + | |
38 | + /*! | |
39 | + コンストラクタ | |
40 | + */ | |
41 | + CIStringWrap(LPCU8STR str) : m_str(m_buffer) | |
42 | + { | |
43 | + m_buffer = str; | |
44 | + } | |
45 | + | |
46 | + ///////////////////////////////////////////////////////////////// | |
47 | + // 操作 | |
48 | + ///////////////////////////////////////////////////////////////// | |
49 | + /*! | |
50 | + 文字列を設定 | |
51 | + */ | |
52 | + virtual void SetString(LPCU8STR str, int length) | |
53 | + { | |
54 | + m_str.SetString(str, length); | |
55 | + } | |
56 | + | |
57 | + /*! | |
58 | + 取得 | |
59 | + */ | |
60 | + virtual LPCU8STR GetString() const | |
61 | + { | |
62 | + return m_str.GetString(); | |
63 | + } | |
64 | + | |
65 | + /*! | |
66 | + 長さ取得 | |
67 | + */ | |
68 | + virtual int GetLength() const | |
69 | + { | |
70 | + return m_str.GetLength(); | |
71 | + } | |
72 | + | |
73 | + /*! | |
74 | + 空? | |
75 | + */ | |
76 | + int IsEmpty() | |
77 | + { | |
78 | + return m_str.IsEmpty(); | |
79 | + } | |
80 | + | |
81 | + /*! | |
82 | + 取得 | |
83 | + */ | |
84 | + operator CUTF8String() | |
85 | + { | |
86 | + return m_str; | |
87 | + } | |
88 | + | |
89 | + /*! | |
90 | + 設定 | |
91 | + */ | |
92 | + void Attach(CUTF8String &str) | |
93 | + { | |
94 | + m_str = str; | |
95 | + } | |
96 | + | |
97 | +protected: | |
98 | + ///////////////////////////////////////////////////////////////// | |
99 | + // メンバ変数 | |
100 | + ///////////////////////////////////////////////////////////////// | |
101 | + //! 参照 | |
102 | + CUTF8String &m_str; | |
103 | + | |
104 | + //! 内部バッファ | |
105 | + CUTF8String m_buffer; | |
106 | +}; | |
107 | + | |
108 | + | |
109 | +/*! | |
110 | + 文字列配列ラッパクラス | |
111 | +*/ | |
112 | +class CIStringArrayWrap : public IStringArray | |
113 | +{ | |
114 | +public: | |
115 | + CIStringArrayWrap(CUTF8StringArray &arr) : | |
116 | + m_array(arr) | |
117 | + { | |
118 | + }; | |
119 | + | |
120 | + //! 初期化 | |
121 | + virtual void Clear() | |
122 | + { | |
123 | + m_array.RemoveAll(); | |
124 | + }; | |
125 | + | |
126 | + //! サイズ設定 | |
127 | + virtual void SetSize(int size) | |
128 | + { | |
129 | + m_array.SetSize(size); | |
130 | + }; | |
131 | + | |
132 | + //! サイズ取得 | |
133 | + virtual int GetSize() | |
134 | + { | |
135 | + return m_array.GetCount(); | |
136 | + }; | |
137 | + | |
138 | + //! 保持している文字列を取得 | |
139 | + virtual void AddString(LPCU8STR str) | |
140 | + { | |
141 | + m_array.Add(CUTF8String(str)); | |
142 | + }; | |
143 | + | |
144 | + //! 保持している文字列を取得 | |
145 | + virtual LPCU8STR GetString(int index) | |
146 | + { | |
147 | + return m_array[index]; | |
148 | + }; | |
149 | + | |
150 | + //! 保持している文字列を取得 | |
151 | + virtual void SetString(int index, LPCU8STR str) | |
152 | + { | |
153 | + m_array[index] = str; | |
154 | + }; | |
155 | + | |
156 | + ///////////////////////////////////////////////////////////////////// | |
157 | + // 補助 | |
158 | + ///////////////////////////////////////////////////////////////////// | |
159 | + //! CUTF8StringArray取得 | |
160 | + CUTF8StringArray &GetStringArray() { return m_array; }; | |
161 | + | |
162 | +protected: | |
163 | + //! 保持中の配列 | |
164 | + CUTF8StringArray &m_array; | |
165 | +}; |
@@ -7,7 +7,8 @@ | ||
7 | 7 | 設定など、所持するプロパティが決まっている場合は、継承してテンプレートとなるクラスを作成できる。 |
8 | 8 | */ |
9 | 9 | #pragma once |
10 | -#include "WorkspaceHelper.h" | |
10 | +#include "types/WorkspaceTypes.h" | |
11 | +#include "IStringHelper.h" | |
11 | 12 | |
12 | 13 | /*! |
13 | 14 | CWorkspace アクセスクラス |
@@ -9,6 +9,7 @@ | ||
9 | 9 | // 文字列 |
10 | 10 | #include "UTF8String.h" |
11 | 11 | #include "UTF8StringConvert.h" |
12 | +#include "IStringHelper.h" | |
12 | 13 | |
13 | 14 | // ワークスペース |
14 | 15 | #include "Workspace.h" |
@@ -1,5 +1,5 @@ | ||
1 | 1 | #pragma once |
2 | -#include "WorkspaceHelper.h" | |
2 | +#include "IStringHelper.h" | |
3 | 3 | #include "WorkspaceNodeWalker.h" |
4 | 4 | #include "WorkspaceKeyPairArray.h" |
5 | 5 | #include "SimpleArrayTemplate.h" |
@@ -1,140 +0,0 @@ | ||
1 | -#include "StdAfx.h" | |
2 | -#include "NormalListenerConnection.h" | |
3 | -#include "HTTPHeaderParser.h" | |
4 | - | |
5 | - | |
6 | -#define MAX_HEADER_SIZE (16 * 1024) // 16K | |
7 | -#define END_HEADER "\r\n\r\n" | |
8 | - | |
9 | -CNormalListenerConnection::CNormalListenerConnection(SOCKET sock, int timeout) | |
10 | -{ | |
11 | - m_socket.AttachSocket(sock); | |
12 | - m_socket.SetTimeout(timeout); | |
13 | -} | |
14 | - | |
15 | -CNormalListenerConnection::~CNormalListenerConnection(void) | |
16 | -{ | |
17 | - Close(); | |
18 | -} | |
19 | - | |
20 | - | |
21 | -///////////////////////////////////////////////////////////////////// | |
22 | -// IClientConnection | |
23 | -///////////////////////////////////////////////////////////////////// | |
24 | -/*! | |
25 | - 初期化 | |
26 | -*/ | |
27 | -void CNormalListenerConnection::AttacheToThread(IServerToolPtr serverTool, IConnectionThreadStatusPtr threadStatus) | |
28 | -{ | |
29 | - m_threadStatus = threadStatus; | |
30 | - m_socket.SetExBreakEvent(m_threadStatus->GetBreakEvent()); | |
31 | -} | |
32 | - | |
33 | -/*! | |
34 | - 切断 | |
35 | -*/ | |
36 | -void CNormalListenerConnection::Close() | |
37 | -{ | |
38 | - m_socket.Close(); | |
39 | -} | |
40 | - | |
41 | - | |
42 | -/*! | |
43 | - クライアント情報の取得 | |
44 | -*/ | |
45 | -void CNormalListenerConnection::GetClientInfo(IWorkspaceAccessPtr clientInfo) | |
46 | -{ | |
47 | - // ラッパ定義 | |
48 | - CContextClientInfo info = CWorkspaceAccess(clientInfo); | |
49 | - | |
50 | - // クライアント情報 | |
51 | - // ホスト名は自動設定されない | |
52 | - info.SetAcceptedPort(m_socket.GetPort()); | |
53 | - info.SetAcceptedAddress(m_socket.GetAddres()); | |
54 | - info.SetClientPort(m_socket.GetPeerPort()); | |
55 | - info.SetClientAddress(m_socket.GetPeerAddres()); | |
56 | -} | |
57 | - | |
58 | -/*! | |
59 | - 次の接続を待つ | |
60 | - | |
61 | - \return TRUE->検出、FALSE->タイムアウト | |
62 | -*/ | |
63 | -int CNormalListenerConnection::WaitNextRequest(int waitTime) | |
64 | -{ | |
65 | - return m_socket.WaitNextRequest(waitTime); | |
66 | -} | |
67 | - | |
68 | -/*! | |
69 | - 接続中か取得する | |
70 | -*/ | |
71 | -int CNormalListenerConnection::IsConnecting() | |
72 | -{ | |
73 | - return !m_socket.IsDisconnect(); | |
74 | -} | |
75 | - | |
76 | - | |
77 | -///////////////////////////////////////////////////////////////////// | |
78 | -// IFilter | |
79 | -///////////////////////////////////////////////////////////////////// | |
80 | -/*! | |
81 | - ヘッダを受信(throw) | |
82 | -*/ | |
83 | -void CNormalListenerConnection::RecvRequestHeader(IStringPtr _requestHeader) | |
84 | -{ | |
85 | - CString requestHeader; | |
86 | - | |
87 | - // ヘッダ受信 | |
88 | - if(m_socket.RecvProtocolHeader(requestHeader, END_HEADER, MAX_HEADER_SIZE, FALSE)) | |
89 | - throw CConnectionErrorException("HTTPヘッダの受信に失敗しました: " + m_socket.GetLastErrorString()); | |
90 | - | |
91 | - // 設定 | |
92 | - _requestHeader->SetString(requestHeader); | |
93 | -} | |
94 | - | |
95 | - | |
96 | -/*! | |
97 | - ボディを受信(throw) | |
98 | -*/ | |
99 | -int CNormalListenerConnection::RecvRequestBody(IBinaryDataPtr _body, int leftSize) | |
100 | -{ | |
101 | - // ラッパ定義 | |
102 | - CBinaryData body(_body); | |
103 | - | |
104 | - // 受信 | |
105 | - int recved = m_socket.RecvBinary(body, leftSize); | |
106 | - if(m_socket.GetLastError()) | |
107 | - throw CConnectionErrorException("データの受信に失敗しました: " + m_socket.GetLastErrorString()); | |
108 | - | |
109 | - return(recved); | |
110 | -} | |
111 | - | |
112 | - | |
113 | -/*! | |
114 | - ヘッダを送信(throw) | |
115 | -*/ | |
116 | -void CNormalListenerConnection::SendResponseHeader(IStringPtr _responseHeader) | |
117 | -{ | |
118 | - CString responseHeader = _responseHeader->GetString(); | |
119 | - | |
120 | - // レスポンスヘッダ送信 | |
121 | - m_socket.SendString(responseHeader); | |
122 | - if(m_socket.GetLastError()) | |
123 | - throw CConnectionErrorException("HTTPヘッダの送信に失敗しました: " + m_socket.GetLastErrorString()); | |
124 | -} | |
125 | - | |
126 | -/*! | |
127 | - ボディを送信(throw) | |
128 | -*/ | |
129 | -void CNormalListenerConnection::SendResponseBody(IBinaryDataPtr _body) | |
130 | -{ | |
131 | - // ラッパ定義 | |
132 | - CBinaryData body(_body); | |
133 | - | |
134 | - // 送信 | |
135 | - m_socket.SendBinary(body); | |
136 | - if(m_socket.GetLastError()) | |
137 | - throw CConnectionErrorException("データの送信に失敗しました: " + m_socket.GetLastErrorString()); | |
138 | -} | |
139 | - | |
140 | - |
@@ -1,100 +0,0 @@ | ||
1 | -/*! | |
2 | - CListenerBase | |
3 | - | |
4 | - IPv4用系接続受付クラスの基本クラス | |
5 | -*/ | |
6 | - | |
7 | -#pragma once | |
8 | -#include "ThreadSkeleton.h" | |
9 | -#include "ListenSocket.h" | |
10 | -#include "IListener.h" | |
11 | -#include "IServerToolWrap.h" | |
12 | -#include "Settings.h" | |
13 | - | |
14 | -/*! | |
15 | - IPv4用系接続受付クラスの基本クラス | |
16 | -*/ | |
17 | -class CListenerBase : | |
18 | - private CThreadSkeleton | |
19 | -{ | |
20 | -public: | |
21 | - CListenerBase(void); | |
22 | - virtual ~CListenerBase(void); | |
23 | - | |
24 | -protected: | |
25 | - ///////////////////////////////////////////////////////////////////////////////////// | |
26 | - // オーバーロード | |
27 | - ///////////////////////////////////////////////////////////////////////////////////// | |
28 | - //! クラス名取得 | |
29 | - virtual LPCU8STR GetListenerClassName() = 0; | |
30 | - | |
31 | - //! 接続追加 | |
32 | - virtual void OnNewConnection(SOCKET newSock) = 0; | |
33 | - | |
34 | - ///////////////////////////////////////////////////////////////////////////////////// | |
35 | - // 操作 | |
36 | - ///////////////////////////////////////////////////////////////////////////////////// | |
37 | - //! 初期化 | |
38 | - void InitListenerBase(CIServerToolWrap serverTool, int errorRetryTime); | |
39 | - | |
40 | - //! 開始 | |
41 | - void StartListenerBase(); | |
42 | - | |
43 | - //! 停止 | |
44 | - void StopListenerBase(); | |
45 | - | |
46 | - //! 設定からポート追加 | |
47 | - void AddPortsFromSetting(CUTF8String strPorts); | |
48 | - | |
49 | - //! ポートの追加 | |
50 | - void AddPort(int port); | |
51 | - | |
52 | - //! ポート数取得 | |
53 | - int GetPortCount() { return m_listeners.GetCount(); }; | |
54 | - | |
55 | - //! 一時的に受け付け停止 | |
56 | - void ForceDisableNewConnection(int time); | |
57 | - | |
58 | -private: | |
59 | - ///////////////////////////////////////////////////////////////////////////////////// | |
60 | - // スレッド関数 | |
61 | - ///////////////////////////////////////////////////////////////////////////////////// | |
62 | - //! スレッド | |
63 | - void ThreadMain(); | |
64 | - | |
65 | - //! クリーンアップ | |
66 | - void CloseAllPorts(); | |
67 | - | |
68 | - //! 受付ループ | |
69 | - int AcceptLoop(); | |
70 | - | |
71 | - //! ポートを開く | |
72 | - int OpenPorts(); | |
73 | - | |
74 | - //! ポートを閉じる | |
75 | - void ClosePorts(); | |
76 | - | |
77 | - //! 接続を受け付け | |
78 | - int AcceptConnections(); | |
79 | - | |
80 | - //! 接続が来るのを待つ | |
81 | - void WaitNextRequest(); | |
82 | - | |
83 | - //! 停止中? | |
84 | - int InDisableMode(); | |
85 | - | |
86 | - ///////////////////////////////////////////////////////////////////////////////////// | |
87 | - // メンバ変数 | |
88 | - ///////////////////////////////////////////////////////////////////////////////////// | |
89 | - //! サーバツール | |
90 | - CIServerToolWrap m_serverTool; | |
91 | - | |
92 | - //! リスナ配列 | |
93 | - CPtrArrayTemplate<CListenSocket*> m_listeners; | |
94 | - | |
95 | - //! エラーリトライ時間 | |
96 | - int m_errorRetryTime; | |
97 | - | |
98 | - //! 受付無効終了予定 | |
99 | - DWORD m_disableNewConnectionEndTime; | |
100 | -}; |
@@ -1,79 +0,0 @@ | ||
1 | -/*! | |
2 | - CNormalListenerConnection | |
3 | - | |
4 | - IPv4用の接続管理クラス | |
5 | -*/ | |
6 | - | |
7 | -#pragma once | |
8 | -#include "IClientConnection.h" | |
9 | -#include "ProtocolSocket.h" | |
10 | - | |
11 | -/*! | |
12 | - ノーマルリスナ クライアントインタフェース | |
13 | - | |
14 | - 通常のHTTPの通信クラス | |
15 | - throw CConnectionErrorException | |
16 | - throw CServerResponse | |
17 | -*/ | |
18 | -class CIPv4ClientConnection : public IClientConnection | |
19 | -{ | |
20 | -public: | |
21 | - //! コンストラクタ | |
22 | - CNormalListenerConnection(SOCKET sock, int timeout); | |
23 | - | |
24 | - //! デストラクタ | |
25 | - virtual ~CNormalListenerConnection(); | |
26 | - | |
27 | -protected: | |
28 | - ////////////////////////////////////////////////////////////////////////////////////////////// | |
29 | - // IClientConnection | |
30 | - ////////////////////////////////////////////////////////////////////////////////////////////// | |
31 | - //! 初期化 | |
32 | - virtual void AttacheToThread(IServerToolPtr serverTool, IConnectionThreadStatusPtr threadStatus); | |
33 | - | |
34 | - //! 初期化 | |
35 | - virtual void Close(); | |
36 | - | |
37 | - //! 削除 | |
38 | - virtual void Delete() { delete this; }; | |
39 | - | |
40 | - | |
41 | - //! クライアント情報の取得 | |
42 | - virtual void GetClientInfo(IWorkspaceAccessPtr clientInfo); | |
43 | - | |
44 | - //! 次の接続を待つ | |
45 | - virtual int WaitNextRequest(int waitTime); | |
46 | - | |
47 | - //! 接続中か取得する(内部でIConnectionThreadStatusPtr::IsConnectingの使用厳禁) | |
48 | - virtual int IsConnecting(); | |
49 | - | |
50 | - | |
51 | - //! ヘッダを受信(throw) | |
52 | - virtual void RecvRequestHeader(IStringPtr requestHeader); | |
53 | - | |
54 | - //! ボディを受信(throw) | |
55 | - virtual int RecvRequestBody(IBinaryDataPtr body, int blockSize); | |
56 | - | |
57 | - //! 受信処理完了 | |
58 | - virtual void EndRecvRequest() {}; | |
59 | - | |
60 | - | |
61 | - //! ヘッダを送信(throw) | |
62 | - virtual void SendResponseHeader(IStringPtr responseHeader); | |
63 | - | |
64 | - //! ボディを送信(throw) | |
65 | - virtual void SendResponseBody(IBinaryDataPtr body); | |
66 | - | |
67 | - //! 送信処理完了 | |
68 | - virtual void EndSendResponse() {}; | |
69 | - | |
70 | - | |
71 | - ///////////////////////////////////////////////////////////////////// | |
72 | - // メンバ変数 | |
73 | - ///////////////////////////////////////////////////////////////////// | |
74 | - //! ソケット | |
75 | - CProtocolSocket m_socket; | |
76 | - | |
77 | - //! 親スレッドの状態 | |
78 | - IConnectionThreadStatusPtr m_threadStatus; | |
79 | -}; |
@@ -1,285 +0,0 @@ | ||
1 | -#include "StdAfx.h" | |
2 | -#include "ListenerBase.h" | |
3 | -#include "ServerException.h" | |
4 | -#include "TextTools.h" | |
5 | - | |
6 | - | |
7 | -CListenerBase::CListenerBase(void) | |
8 | -{ | |
9 | -} | |
10 | - | |
11 | -CListenerBase::~CListenerBase(void) | |
12 | -{ | |
13 | - StopListenerBase(); | |
14 | -} | |
15 | - | |
16 | - | |
17 | - | |
18 | -///////////////////////////////////////////////////////////////////////////////////// | |
19 | -// 操作 | |
20 | -///////////////////////////////////////////////////////////////////////////////////// | |
21 | -/*! | |
22 | - 初期化 | |
23 | -*/ | |
24 | -void CListenerBase::InitListenerBase(CIServerToolWrap serverTool, int errorRetryTime) | |
25 | -{ | |
26 | - m_serverTool = serverTool; | |
27 | - m_errorRetryTime = errorRetryTime; | |
28 | - m_disableNewConnectionEndTime = 0; | |
29 | -} | |
30 | - | |
31 | -/*! | |
32 | - 開始 | |
33 | -*/ | |
34 | -void CListenerBase::StartListenerBase() | |
35 | -{ | |
36 | - if(OpenPorts()) | |
37 | - throw CServerStartErrorException(GetListenerClassName(), _U16("接続の待ち受け開始に失敗しました")); | |
38 | - | |
39 | - if(StartThread()) | |
40 | - throw CServerStartErrorException(GetListenerClassName(), _U16("スレッドの起動に失敗しました")); | |
41 | -} | |
42 | - | |
43 | -/*! | |
44 | - 停止 | |
45 | -*/ | |
46 | -void CListenerBase::StopListenerBase() | |
47 | -{ | |
48 | - // スレッド停止 | |
49 | - EndThread(); | |
50 | - | |
51 | - // 掃除 | |
52 | - CloseAllPorts(); | |
53 | -} | |
54 | - | |
55 | -/*! | |
56 | - クリーンアップ | |
57 | -*/ | |
58 | -void CListenerBase::CloseAllPorts() | |
59 | -{ | |
60 | - // 掃除 | |
61 | - for(int i=0;i<m_listeners.GetCount();i++) | |
62 | - delete m_listeners[i]; | |
63 | - m_listeners.RemoveAll(); | |
64 | -} | |
65 | - | |
66 | -/*! | |
67 | - 設定からポート追加 | |
68 | -*/ | |
69 | -void CListenerBase::AddPortsFromSetting(CUTF8String strPorts) | |
70 | -{ | |
71 | - // ポート列取得 | |
72 | - CUTF8StringArray ports; | |
73 | - CTextTools::GetAllToken(strPorts, ports, _U8(","), TRUE); | |
74 | - | |
75 | - // 登録 | |
76 | - for(int i=0;i<ports.GetCount();i++) | |
77 | - { | |
78 | - // ポート番号取得 | |
79 | - int port = CTextTools::StrTol(ports[i]); | |
80 | - if(port > 0 && port < 65536) | |
81 | - AddPort(port); | |
82 | - } | |
83 | -} | |
84 | - | |
85 | -/*! | |
86 | - ポートの追加 | |
87 | -*/ | |
88 | -void CListenerBase::AddPort(int port) | |
89 | -{ | |
90 | - // 禁止 | |
91 | - if(IsRunning()) | |
92 | - return; | |
93 | - | |
94 | - // 追加 | |
95 | - m_listeners.Add(new CListenSocket(port)); | |
96 | - | |
97 | - // ログへ | |
98 | - m_serverTool.WriteSystemLog(GetListenerClassName(), SYSTEM_LOG_INFO, CUTF8String::GetFormat(_U16("ポート %d を追加しました"), port)); | |
99 | - | |
100 | -} | |
101 | - | |
102 | - | |
103 | -/*! | |
104 | - 一時的に受け付け停止 | |
105 | -*/ | |
106 | -void CListenerBase::ForceDisableNewConnection(int time) | |
107 | -{ | |
108 | - m_disableNewConnectionEndTime = GetTickCount() + time; | |
109 | -} | |
110 | - | |
111 | -///////////////////////////////////////////////////////////////////////////////////// | |
112 | -// スレッド関数 | |
113 | -///////////////////////////////////////////////////////////////////////////////////// | |
114 | -/*! | |
115 | - スレッド | |
116 | -*/ | |
117 | -void CListenerBase::ThreadMain() | |
118 | -{ | |
119 | - // エラーループ | |
120 | - while(!GetBreak()) | |
121 | - { | |
122 | - // 許可ループ | |
123 | - AcceptLoop(); | |
124 | - | |
125 | - // ポートの待ち受けをやり直し | |
126 | - while(!GetBreak() && OpenPorts()) | |
127 | - WaitForSomeObjects(m_errorRetryTime, GetBreakEvent()); // エラーなので、しばらく待機 | |
128 | - } | |
129 | - | |
130 | - // 閉じる | |
131 | - ClosePorts(); | |
132 | - | |
133 | - // 最終ステータス | |
134 | - m_serverTool.WriteSystemLog(GetListenerClassName(), SYSTEM_LOG_INFO, _U16("ポート待ち受けを停止しました")); | |
135 | -} | |
136 | - | |
137 | -/*! | |
138 | - 受付ループ | |
139 | -*/ | |
140 | -int CListenerBase::AcceptLoop() | |
141 | -{ | |
142 | - // 待ち受けループ | |
143 | - while(!GetBreak()) | |
144 | - { | |
145 | - // 許可を試みる | |
146 | - if(AcceptConnections()) | |
147 | - return(-1); // エラー | |
148 | - | |
149 | - // イベントを待つ | |
150 | - WaitNextRequest(); | |
151 | - } | |
152 | - | |
153 | - return(0); | |
154 | -} | |
155 | - | |
156 | - | |
157 | -/*! | |
158 | - ポートを開く | |
159 | -*/ | |
160 | -int CListenerBase::OpenPorts() | |
161 | -{ | |
162 | - // まずは、すべて閉じる | |
163 | - ClosePorts(); | |
164 | - | |
165 | - // 全てのポートを開く | |
166 | - for(int i=0;i<m_listeners.GetCount();i++) | |
167 | - { | |
168 | - // 待ち受け開始 | |
169 | - if(m_listeners[i]->StartListen()) | |
170 | - { | |
171 | - // エラー報告 | |
172 | - m_serverTool.WriteSystemLog(GetListenerClassName(), SYSTEM_LOG_ERROR, | |
173 | - CUTF8String::GetFormat(_U16("ポート %d の待ち受け開始に失敗しました"), m_listeners[i]->GetPort())); | |
174 | - return(-1); | |
175 | - } | |
176 | - } | |
177 | - | |
178 | - // 成功報告 | |
179 | - m_serverTool.WriteSystemLog(GetListenerClassName(), SYSTEM_LOG_INFO, _U16("全ポートの待ち受けを開始しました")); | |
180 | - return(0); | |
181 | -} | |
182 | - | |
183 | -/*! | |
184 | - ポートを閉じる | |
185 | -*/ | |
186 | -void CListenerBase::ClosePorts() | |
187 | -{ | |
188 | - // 全てのポートを閉じる | |
189 | - for(int i=0;i<m_listeners.GetCount();i++) | |
190 | - { | |
191 | - // 待ち受け停止 | |
192 | - m_listeners[i]->EndListen(); | |
193 | - } | |
194 | -} | |
195 | - | |
196 | - | |
197 | -/*! | |
198 | - 接続を受け付け | |
199 | -*/ | |
200 | -int CListenerBase::AcceptConnections() | |
201 | -{ | |
202 | - // テンポラリ | |
203 | - SOCKET newSock; | |
204 | - CIPAddress ip; | |
205 | - | |
206 | - while(1) | |
207 | - { | |
208 | - // 全てのポートをチェック | |
209 | - int acceptedCount = 0; | |
210 | - for(int i=0;i<m_listeners.GetCount();i++) | |
211 | - { | |
212 | - // 受付を試みる | |
213 | - switch(m_listeners[i]->TryAccept(newSock, ip)) | |
214 | - { | |
215 | - // ソケットなし | |
216 | - case LISTEN_SOCKET_NOCLIENT: | |
217 | - break; | |
218 | - | |
219 | - // エラー | |
220 | - case LISTEN_SOCKET_ERROR: | |
221 | - m_serverTool.WriteSystemLog(GetListenerClassName(), SYSTEM_LOG_ERROR, | |
222 | - CUTF8String::GetFormat(_U16("ポート %s の接続受付処理でエラーが発生しました"), m_listeners[i]->GetPort())); | |
223 | - return(-1); | |
224 | - | |
225 | - // 新規接続 | |
226 | - case LISTEN_SOCKET_ACCEPTED: | |
227 | - acceptedCount++; | |
228 | - | |
229 | - // 無効? | |
230 | - if(InDisableMode()) | |
231 | - closesocket(newSock); // 削除 | |
232 | - else | |
233 | - OnNewConnection(newSock); | |
234 | - | |
235 | - TRACE_U8(_U16("New connection\n")); | |
236 | - break; | |
237 | - } | |
238 | - } | |
239 | - | |
240 | - // 当面無い | |
241 | - if(acceptedCount == 0) | |
242 | - return(0); | |
243 | - } | |
244 | -} | |
245 | - | |
246 | - | |
247 | - | |
248 | -/*! | |
249 | - 接続が来るのを待つ | |
250 | -*/ | |
251 | -void CListenerBase::WaitNextRequest() | |
252 | -{ | |
253 | - // イベント数 | |
254 | - int eventCount = m_listeners.GetCount(); | |
255 | - if(eventCount > MAXIMUM_WAIT_OBJECTS - 1) | |
256 | - eventCount = MAXIMUM_WAIT_OBJECTS - 1; | |
257 | - | |
258 | - // すべてのイベントを取得 | |
259 | - HANDLE waits[MAXIMUM_WAIT_OBJECTS]; | |
260 | - for(int i=0;i<eventCount;i++) | |
261 | - waits[i+1] = m_listeners[i]->GetEventHandle(); | |
262 | - | |
263 | - // 待つ | |
264 | - waits[0] = GetBreakEvent(); | |
265 | - ::WaitForMultipleObjects(eventCount + 1, waits, FALSE, INFINITE); | |
266 | -} | |
267 | - | |
268 | - | |
269 | -/*! | |
270 | - 停止中? | |
271 | -*/ | |
272 | -int CListenerBase::InDisableMode() | |
273 | -{ | |
274 | - if(m_disableNewConnectionEndTime != 0) | |
275 | - { | |
276 | - DWORD tick = GetTickCount(); | |
277 | - if(m_disableNewConnectionEndTime < tick || m_disableNewConnectionEndTime >= tick) | |
278 | - { | |
279 | - m_disableNewConnectionEndTime = 0; | |
280 | - return(0); | |
281 | - } | |
282 | - return(1); | |
283 | - } | |
284 | - return(0); | |
285 | -} |
@@ -0,0 +1,229 @@ | ||
1 | +#include "StdAfx.h" | |
2 | +#include "HTTPHeaderParser.h" | |
3 | +#include "TextTools.h" | |
4 | +#include "UrlUtility.h" | |
5 | + | |
6 | +/*! | |
7 | + リクエストヘッダーの解析を行う | |
8 | +*/ | |
9 | +int CHTTPHeaderParser::ParseRequestHeader(CUTF8String &header, CContextRequestInfo requestInfo) | |
10 | +{ | |
11 | + CUTF8String line; | |
12 | + CUTF8String method; | |
13 | + CUTF8String url; | |
14 | + CUTF8String object; | |
15 | + CUTF8String query; | |
16 | + CUTF8String queryDecoded; | |
17 | + CUTF8String version; | |
18 | + CUTF8String user; | |
19 | + CUTF8String passwd; | |
20 | + | |
21 | + // リクエスト取り出し | |
22 | + if(CTextTools::GetLine(header, line)==0) | |
23 | + return(-1); | |
24 | + | |
25 | + // メソッド | |
26 | + if(CTextTools::GetToken(line, method, _U8(" "))==0) | |
27 | + return(-1); | |
28 | + | |
29 | + // URL | |
30 | + if(CTextTools::GetToken(line, url, _U8(" "))==0) | |
31 | + return(-1); | |
32 | + | |
33 | + // URIObject | |
34 | + object = CUrlUtility::GetUrlObject(url); | |
35 | + object = CUrlUtility::DecodeUrl(object); | |
36 | + | |
37 | + // Query | |
38 | + query = CUrlUtility::GetQuery(url); | |
39 | + queryDecoded = CUrlUtility::Decode(query); | |
40 | + | |
41 | + // バージョン | |
42 | + if(CTextTools::GetToken(line, version,_U8("/"))==0) | |
43 | + return(-1); | |
44 | + if(line.GetLength() == 0) | |
45 | + return(-1); | |
46 | + version = line; | |
47 | + | |
48 | + // 設定 | |
49 | + requestInfo.Clear(); | |
50 | + requestInfo.SetMethod(method); | |
51 | + requestInfo.SetURL(url); | |
52 | + requestInfo.SetObject(object); | |
53 | + requestInfo.SetQuery(query); | |
54 | + requestInfo.SetQueryDecoded(queryDecoded); | |
55 | + requestInfo.SetVersion(version); | |
56 | + | |
57 | + | |
58 | + // オプションヘッダー取得 | |
59 | + CContextRequestOption options = requestInfo.GetRequestOption(); | |
60 | + CUTF8String key; | |
61 | + while(1) | |
62 | + { | |
63 | + if(CTextTools::GetLine(header,line)==0) | |
64 | + break; | |
65 | + | |
66 | + // key | |
67 | + if(CTextTools::GetToken(line,key,_U8(": "))==0) // xxx: xxxx | |
68 | + if(CTextTools::GetToken(line,key,_U8(":"))==0) // xxx:xxxx | |
69 | + continue; | |
70 | + | |
71 | + // data | |
72 | + if(line.GetLength() == 0) | |
73 | + continue; | |
74 | + | |
75 | + options.SetKeyStr(key,line); | |
76 | + } | |
77 | + return(0); | |
78 | +} | |
79 | + | |
80 | + | |
81 | +/*! | |
82 | + レスポンスヘッダの生成を行う | |
83 | +*/ | |
84 | +CUTF8String CHTTPHeaderParser::GetResponseHeader(CContextResponseInfo responseInfo) | |
85 | +{ | |
86 | + CContextResponseOption responseOptions = responseInfo.GetResponseOption(); | |
87 | + | |
88 | + // レスポンスライン | |
89 | + CUTF8String header,line; | |
90 | + line.Format(_U8("HTTP/1.1 %s\r\n"), GetResponseText(responseInfo.GetResponseCode(), responseInfo.GetResponseText())); | |
91 | + header += line; | |
92 | + | |
93 | + // オプション | |
94 | + CStringArray keyList; | |
95 | + responseOptions.GetAllKey(keyList); | |
96 | + for(int i=0;i<keyList.GetSize();i++) | |
97 | + { | |
98 | + if(!responseOptions.GetKeyStr(keyList[i], _U8("")).IsEmpty()) | |
99 | + { | |
100 | + line.Format(_U16("%s: %s\r\n"), keyList[i],responseOptions.GetKeyStr(keyList[i], _U8(""))); | |
101 | + header += line; | |
102 | + } | |
103 | + } | |
104 | + | |
105 | + // CGI用ヘッダ | |
106 | + header += responseInfo.GetCGIHeader(); | |
107 | + header.TrimRight(); | |
108 | + header += _U8("\r\n\r\n"); | |
109 | + | |
110 | + return(header); | |
111 | +} | |
112 | + | |
113 | + | |
114 | +/*! | |
115 | + レスポンステキスト取得 | |
116 | +*/ | |
117 | +CUTF8String CHTTPHeaderParser::GetResponseText(int code, CUTF8String responseText) | |
118 | +{ | |
119 | + // responseTextが設定されている? | |
120 | + if(!responseText.IsEmpty()) | |
121 | + { | |
122 | + responseText.Replace(_U8("\r"), _U8("")); | |
123 | + responseText.Replace(_U8("\n"), _U8("")); | |
124 | + | |
125 | + // 数字指定? | |
126 | + if(responseText.GetLength() > 4 && responseText[3] == ' ' && atoi(responseText.Left(3)) >= 100) | |
127 | + return(responseText); | |
128 | + else | |
129 | + return(CTextTools::GetFormat(_U16("%d %s"),code,responseText)); | |
130 | + } | |
131 | + | |
132 | + // 設定 | |
133 | + switch(code) | |
134 | + { | |
135 | + // 成功 | |
136 | + case 100: | |
137 | + return(_U8("100 Continue")); | |
138 | + case 101: | |
139 | + return(_U8("101 Switching Protocols")); | |
140 | + case 200: | |
141 | + return(_U8("200 OK")); | |
142 | + case 201: | |
143 | + return(_U8("201 Created")); | |
144 | + case 202: | |
145 | + return(_U8("202 Accepted")); | |
146 | + case 203: | |
147 | + return(_U8("203 Non-Authoritative Information")); | |
148 | + case 204: | |
149 | + return(_U8("204 No Content")); | |
150 | + case 205: | |
151 | + return(_U8("205 Reset Content")); | |
152 | + case 206: | |
153 | + return(_U8("206 Partial Content")); | |
154 | + case 207: | |
155 | + return(_U8("207 Multi-Status")); | |
156 | + | |
157 | + // エラー(30xは自動に処理される) | |
158 | + case 300: | |
159 | + return(_U8("300 Multiple Choices")); | |
160 | + case 301: | |
161 | + return(_U8("301 Moved Permanently")); | |
162 | + case 302: | |
163 | + return(_U8("302 Found")); | |
164 | + case 303: | |
165 | + return(_U8("303 See Other")); | |
166 | + case 304: | |
167 | + return(_U8("304 Not Modified")); | |
168 | + case 305: | |
169 | + return(_U8("305 Use Proxy")); | |
170 | + case 306: | |
171 | + return(_U8("306 (Unused)")); | |
172 | + case 307: | |
173 | + return(_U8("307 Temporary Redirect")); | |
174 | + | |
175 | + | |
176 | + case 400: | |
177 | + return(_U8("400 Bad Request")); | |
178 | + case 401: | |
179 | + return(_U8("401 Unauthorized")); | |
180 | + case 402: | |
181 | + return(_U8("402 Payment Required")); | |
182 | + case 403: | |
183 | + return(_U8("403 Forbidden")); | |
184 | + case 404: | |
185 | + return(_U8("404 Not Found")); | |
186 | + case 405: | |
187 | + return(_U8("405 Method Not Allowed")); | |
188 | + case 406: | |
189 | + return(_U8("406 Not Acceptable")); | |
190 | + case 407: | |
191 | + return(_U8("407 Proxy Authentication Required")); | |
192 | + case 408: | |
193 | + return(_U8("408 Request Timeout")); | |
194 | + case 409: | |
195 | + return(_U8("409 Conflict")); | |
196 | + case 410: | |
197 | + return(_U8("410 Gone")); | |
198 | + case 411: | |
199 | + return(_U8("411 Length Required")); | |
200 | + case 412: | |
201 | + return(_U8("412 Precondition Failed")); | |
202 | + case 413: | |
203 | + return(_U8("413 Request Entity Too Large")); | |
204 | + case 414: | |
205 | + return(_U8("414 Request-URI Too Long")); | |
206 | + case 415: | |
207 | + return(_U8("415 Unsupported Media Type")); | |
208 | + case 416: | |
209 | + return(_U8("416 Requested Range Not Satisfiable")); | |
210 | + | |
211 | + | |
212 | + case 500: | |
213 | + return(_U8("500 Internal Server Error")); | |
214 | + case 501: | |
215 | + return(_U8("501 Not Implemented")); | |
216 | + case 502: | |
217 | + return(_U8("502 Bad Gateway")); | |
218 | + case 503: | |
219 | + return(_U8("503 Service Unavailable")); | |
220 | + case 504: | |
221 | + return(_U8("504 Gateway Timeout")); | |
222 | + case 505: | |
223 | + return(_U8("505 HTTP Version Not Supported")); | |
224 | + } | |
225 | + | |
226 | + CUTF8String unknown; | |
227 | + unknown.Format(_U16("%d UNKNOWN"),code); | |
228 | + return(unknown); | |
229 | +} |
@@ -0,0 +1,82 @@ | ||
1 | +/*! | |
2 | + CIPv4ClientConnection | |
3 | + | |
4 | + IPv4用の接続管理クラス | |
5 | +*/ | |
6 | + | |
7 | +#pragma once | |
8 | +#include "IClientConnection.h" | |
9 | +#include "ProtocolSocket.h" | |
10 | + | |
11 | +/*! | |
12 | + ノーマルリスナ クライアントインタフェース | |
13 | + | |
14 | + 通常のHTTPの通信クラス | |
15 | + throw CConnectionErrorException | |
16 | + throw CServerResponse | |
17 | +*/ | |
18 | +class CIPv4ClientConnection : public IClientConnection | |
19 | +{ | |
20 | +public: | |
21 | + ////////////////////////////////////////////////////////////////////////////////////////////// | |
22 | + // コンストラクタ | |
23 | + ////////////////////////////////////////////////////////////////////////////////////////////// | |
24 | + //! コンストラクタ | |
25 | + CIPv4ClientConnection(SOCKET sock, int timeout); | |
26 | + | |
27 | + //! デストラクタ | |
28 | + virtual ~CIPv4ClientConnection(); | |
29 | + | |
30 | +protected: | |
31 | + ////////////////////////////////////////////////////////////////////////////////////////////// | |
32 | + // IClientConnection | |
33 | + ////////////////////////////////////////////////////////////////////////////////////////////// | |
34 | + //! 初期化 | |
35 | + virtual void AttacheToThread(IServerToolPtr serverTool, IConnectionThreadStatusPtr threadStatus); | |
36 | + | |
37 | + //! 初期化 | |
38 | + virtual void Close(); | |
39 | + | |
40 | + //! 削除 | |
41 | + virtual void Delete() { delete this; }; | |
42 | + | |
43 | + | |
44 | + //! クライアント情報の取得 | |
45 | + virtual void GetClientInfo(IWorkspaceAccessPtr clientInfo); | |
46 | + | |
47 | + //! 次の接続を待つ | |
48 | + virtual int WaitNextRequest(int waitTime); | |
49 | + | |
50 | + //! 接続中か取得する(内部でIConnectionThreadStatusPtr::IsConnectingの使用厳禁) | |
51 | + virtual int IsConnecting(); | |
52 | + | |
53 | + | |
54 | + //! ヘッダを受信(throw) | |
55 | + virtual void RecvRequestHeader(IStringPtr requestHeader); | |
56 | + | |
57 | + //! ボディを受信(throw) | |
58 | + virtual int RecvRequestBody(IBinaryDataPtr body, int blockSize); | |
59 | + | |
60 | + //! 受信処理完了 | |
61 | + virtual void EndRecvRequest() {}; | |
62 | + | |
63 | + | |
64 | + //! ヘッダを送信(throw) | |
65 | + virtual void SendResponseHeader(IStringPtr responseHeader); | |
66 | + | |
67 | + //! ボディを送信(throw) | |
68 | + virtual void SendResponseBody(IBinaryDataPtr body); | |
69 | + | |
70 | + //! 送信処理完了 | |
71 | + virtual void EndSendResponse() {}; | |
72 | + | |
73 | + | |
74 | + ///////////////////////////////////////////////////////////////////// | |
75 | + // メンバ変数 | |
76 | + ///////////////////////////////////////////////////////////////////// | |
77 | + //! ソケット | |
78 | + CProtocolSocket m_socket; | |
79 | + | |
80 | + //! 親スレッドの状態 | |
81 | + IConnectionThreadStatusPtr m_threadStatus; | |
82 | +}; |
@@ -53,6 +53,9 @@ | ||
53 | 53 | //! DWORD 取得 |
54 | 54 | operator DWORD() const; |
55 | 55 | |
56 | + //! 文字列取得 取得 | |
57 | + operator CUTF8String() const { return GetString(); } | |
58 | + | |
56 | 59 | //! 文字列取得 |
57 | 60 | CUTF8String GetString() const; |
58 | 61 |
@@ -105,20 +105,20 @@ | ||
105 | 105 | }; |
106 | 106 | |
107 | 107 | /*! |
108 | - ノーマルリスナの設定 | |
108 | + IPv4リスナの設定 | |
109 | 109 | Manager |
110 | 110 | |
111 | 111 | 通常の接続に関する設定を兼ねる |
112 | 112 | */ |
113 | -class CNormalListenerSetting : | |
113 | +class CIPv4ListenerSetting : | |
114 | 114 | protected CWorkspaceAccess |
115 | 115 | { |
116 | 116 | public: |
117 | 117 | // コンストラクタをまとめて定義 |
118 | - DEFINE_COMMON_CONSTRUCTOR(CNormalListenerSetting); | |
118 | + DEFINE_COMMON_CONSTRUCTOR(CIPv4ListenerSetting); | |
119 | 119 | |
120 | 120 | //! 名称取得 |
121 | - CUTF8String GetSettingName() { return _U8("NormalListener"); }; | |
121 | + CUTF8String GetSettingName() { return _U8("IPv4Listener"); }; | |
122 | 122 | |
123 | 123 | /////////////////////////////////////////////////////////////////////////////////////////////////// |
124 | 124 | // プロパティ |
@@ -0,0 +1,285 @@ | ||
1 | +#include "StdAfx.h" | |
2 | +#include "IPv4ListenerBase.h" | |
3 | +#include "ServerException.h" | |
4 | +#include "TextTools.h" | |
5 | + | |
6 | + | |
7 | +CIPv4ListenerBase::CIPv4ListenerBase(void) | |
8 | +{ | |
9 | +} | |
10 | + | |
11 | +CIPv4ListenerBase::~CIPv4ListenerBase(void) | |
12 | +{ | |
13 | + StopListenerBase(); | |
14 | +} | |
15 | + | |
16 | + | |
17 | + | |
18 | +///////////////////////////////////////////////////////////////////////////////////// | |
19 | +// 操作 | |
20 | +///////////////////////////////////////////////////////////////////////////////////// | |
21 | +/*! | |
22 | + 初期化 | |
23 | +*/ | |
24 | +void CIPv4ListenerBase::InitListenerBase(CIServerToolWrap serverTool, int errorRetryTime) | |
25 | +{ | |
26 | + m_serverTool = serverTool; | |
27 | + m_errorRetryTime = errorRetryTime; | |
28 | + m_disableNewConnectionEndTime = 0; | |
29 | +} | |
30 | + | |
31 | +/*! | |
32 | + 開始 | |
33 | +*/ | |
34 | +void CIPv4ListenerBase::StartListenerBase() | |
35 | +{ | |
36 | + if(OpenPorts()) | |
37 | + throw CServerStartErrorException(GetListenerClassName(), _U16("接続の待ち受け開始に失敗しました")); | |
38 | + | |
39 | + if(StartThread()) | |
40 | + throw CServerStartErrorException(GetListenerClassName(), _U16("スレッドの起動に失敗しました")); | |
41 | +} | |
42 | + | |
43 | +/*! | |
44 | + 停止 | |
45 | +*/ | |
46 | +void CIPv4ListenerBase::StopListenerBase() | |
47 | +{ | |
48 | + // スレッド停止 | |
49 | + EndThread(); | |
50 | + | |
51 | + // 掃除 | |
52 | + CloseAllPorts(); | |
53 | +} | |
54 | + | |
55 | +/*! | |
56 | + クリーンアップ | |
57 | +*/ | |
58 | +void CIPv4ListenerBase::CloseAllPorts() | |
59 | +{ | |
60 | + // 掃除 | |
61 | + for(int i=0;i<m_listeners.GetCount();i++) | |
62 | + delete m_listeners[i]; | |
63 | + m_listeners.RemoveAll(); | |
64 | +} | |
65 | + | |
66 | +/*! | |
67 | + 設定からポート追加 | |
68 | +*/ | |
69 | +void CIPv4ListenerBase::AddPortsFromSetting(CUTF8String strPorts) | |
70 | +{ | |
71 | + // ポート列取得 | |
72 | + CUTF8StringArray ports; | |
73 | + CTextTools::GetAllToken(strPorts, ports, _U8(","), TRUE); | |
74 | + | |
75 | + // 登録 | |
76 | + for(int i=0;i<ports.GetCount();i++) | |
77 | + { | |
78 | + // ポート番号取得 | |
79 | + int port = CTextTools::StrTol(ports[i]); | |
80 | + if(port > 0 && port < 65536) | |
81 | + AddPort(port); | |
82 | + } | |
83 | +} | |
84 | + | |
85 | +/*! | |
86 | + ポートの追加 | |
87 | +*/ | |
88 | +void CIPv4ListenerBase::AddPort(int port) | |
89 | +{ | |
90 | + // 禁止 | |
91 | + if(IsRunning()) | |
92 | + return; | |
93 | + | |
94 | + // 追加 | |
95 | + m_listeners.Add(new CListenSocket(port)); | |
96 | + | |
97 | + // ログへ | |
98 | + m_serverTool.WriteSystemLog(GetListenerClassName(), SYSTEM_LOG_INFO, CUTF8String::GetFormat(_U16("ポート %d を追加しました"), port)); | |
99 | + | |
100 | +} | |
101 | + | |
102 | + | |
103 | +/*! | |
104 | + 一時的に受け付け停止 | |
105 | +*/ | |
106 | +void CIPv4ListenerBase::ForceDisableNewConnection(int time) | |
107 | +{ | |
108 | + m_disableNewConnectionEndTime = GetTickCount() + time; | |
109 | +} | |
110 | + | |
111 | +///////////////////////////////////////////////////////////////////////////////////// | |
112 | +// スレッド関数 | |
113 | +///////////////////////////////////////////////////////////////////////////////////// | |
114 | +/*! | |
115 | + スレッド | |
116 | +*/ | |
117 | +void CIPv4ListenerBase::ThreadMain() | |
118 | +{ | |
119 | + // エラーループ | |
120 | + while(!GetBreak()) | |
121 | + { | |
122 | + // 許可ループ | |
123 | + AcceptLoop(); | |
124 | + | |
125 | + // ポートの待ち受けをやり直し | |
126 | + while(!GetBreak() && OpenPorts()) | |
127 | + WaitForSomeObjects(m_errorRetryTime, GetBreakEvent()); // エラーなので、しばらく待機 | |
128 | + } | |
129 | + | |
130 | + // 閉じる | |
131 | + ClosePorts(); | |
132 | + | |
133 | + // 最終ステータス | |
134 | + m_serverTool.WriteSystemLog(GetListenerClassName(), SYSTEM_LOG_INFO, _U16("ポート待ち受けを停止しました")); | |
135 | +} | |
136 | + | |
137 | +/*! | |
138 | + 受付ループ | |
139 | +*/ | |
140 | +int CIPv4ListenerBase::AcceptLoop() | |
141 | +{ | |
142 | + // 待ち受けループ | |
143 | + while(!GetBreak()) | |
144 | + { | |
145 | + // 許可を試みる | |
146 | + if(AcceptConnections()) | |
147 | + return(-1); // エラー | |
148 | + | |
149 | + // イベントを待つ | |
150 | + WaitNextRequest(); | |
151 | + } | |
152 | + | |
153 | + return(0); | |
154 | +} | |
155 | + | |
156 | + | |
157 | +/*! | |
158 | + ポートを開く | |
159 | +*/ | |
160 | +int CIPv4ListenerBase::OpenPorts() | |
161 | +{ | |
162 | + // まずは、すべて閉じる | |
163 | + ClosePorts(); | |
164 | + | |
165 | + // 全てのポートを開く | |
166 | + for(int i=0;i<m_listeners.GetCount();i++) | |
167 | + { | |
168 | + // 待ち受け開始 | |
169 | + if(m_listeners[i]->StartListen()) | |
170 | + { | |
171 | + // エラー報告 | |
172 | + m_serverTool.WriteSystemLog(GetListenerClassName(), SYSTEM_LOG_ERROR, | |
173 | + CUTF8String::GetFormat(_U16("ポート %d の待ち受け開始に失敗しました"), m_listeners[i]->GetPort())); | |
174 | + return(-1); | |
175 | + } | |
176 | + } | |
177 | + | |
178 | + // 成功報告 | |
179 | + m_serverTool.WriteSystemLog(GetListenerClassName(), SYSTEM_LOG_INFO, _U16("全ポートの待ち受けを開始しました")); | |
180 | + return(0); | |
181 | +} | |
182 | + | |
183 | +/*! | |
184 | + ポートを閉じる | |
185 | +*/ | |
186 | +void CIPv4ListenerBase::ClosePorts() | |
187 | +{ | |
188 | + // 全てのポートを閉じる | |
189 | + for(int i=0;i<m_listeners.GetCount();i++) | |
190 | + { | |
191 | + // 待ち受け停止 | |
192 | + m_listeners[i]->EndListen(); | |
193 | + } | |
194 | +} | |
195 | + | |
196 | + | |
197 | +/*! | |
198 | + 接続を受け付け | |
199 | +*/ | |
200 | +int CIPv4ListenerBase::AcceptConnections() | |
201 | +{ | |
202 | + // テンポラリ | |
203 | + SOCKET newSock; | |
204 | + CIPAddress ip; | |
205 | + | |
206 | + while(1) | |
207 | + { | |
208 | + // 全てのポートをチェック | |
209 | + int acceptedCount = 0; | |
210 | + for(int i=0;i<m_listeners.GetCount();i++) | |
211 | + { | |
212 | + // 受付を試みる | |
213 | + switch(m_listeners[i]->TryAccept(newSock, ip)) | |
214 | + { | |
215 | + // ソケットなし | |
216 | + case LISTEN_SOCKET_NOCLIENT: | |
217 | + break; | |
218 | + | |
219 | + // エラー | |
220 | + case LISTEN_SOCKET_ERROR: | |
221 | + m_serverTool.WriteSystemLog(GetListenerClassName(), SYSTEM_LOG_ERROR, | |
222 | + CUTF8String::GetFormat(_U16("ポート %s の接続受付処理でエラーが発生しました"), m_listeners[i]->GetPort())); | |
223 | + return(-1); | |
224 | + | |
225 | + // 新規接続 | |
226 | + case LISTEN_SOCKET_ACCEPTED: | |
227 | + acceptedCount++; | |
228 | + | |
229 | + // 無効? | |
230 | + if(InDisableMode()) | |
231 | + closesocket(newSock); // 削除 | |
232 | + else | |
233 | + OnNewConnection(newSock); | |
234 | + | |
235 | + TRACE_U8(_U16("New connection\n")); | |
236 | + break; | |
237 | + } | |
238 | + } | |
239 | + | |
240 | + // 当面無い | |
241 | + if(acceptedCount == 0) | |
242 | + return(0); | |
243 | + } | |
244 | +} | |
245 | + | |
246 | + | |
247 | + | |
248 | +/*! | |
249 | + 接続が来るのを待つ | |
250 | +*/ | |
251 | +void CIPv4ListenerBase::WaitNextRequest() | |
252 | +{ | |
253 | + // イベント数 | |
254 | + int eventCount = m_listeners.GetCount(); | |
255 | + if(eventCount > MAXIMUM_WAIT_OBJECTS - 1) | |
256 | + eventCount = MAXIMUM_WAIT_OBJECTS - 1; | |
257 | + | |
258 | + // すべてのイベントを取得 | |
259 | + HANDLE waits[MAXIMUM_WAIT_OBJECTS]; | |
260 | + for(int i=0;i<eventCount;i++) | |
261 | + waits[i+1] = m_listeners[i]->GetEventHandle(); | |
262 | + | |
263 | + // 待つ | |
264 | + waits[0] = GetBreakEvent(); | |
265 | + ::WaitForMultipleObjects(eventCount + 1, waits, FALSE, INFINITE); | |
266 | +} | |
267 | + | |
268 | + | |
269 | +/*! | |
270 | + 停止中? | |
271 | +*/ | |
272 | +int CIPv4ListenerBase::InDisableMode() | |
273 | +{ | |
274 | + if(m_disableNewConnectionEndTime != 0) | |
275 | + { | |
276 | + DWORD tick = GetTickCount(); | |
277 | + if(m_disableNewConnectionEndTime < tick || m_disableNewConnectionEndTime >= tick) | |
278 | + { | |
279 | + m_disableNewConnectionEndTime = 0; | |
280 | + return(0); | |
281 | + } | |
282 | + return(1); | |
283 | + } | |
284 | + return(0); | |
285 | +} |
@@ -11,6 +11,7 @@ | ||
11 | 11 | DLL内部で一旦例外を拾い、DLLから抜けた後再度投げられれようにしておく |
12 | 12 | */ |
13 | 13 | #pragma once |
14 | +#include "BadAllocException.h" // メモリ例外 | |
14 | 15 | |
15 | 16 | /*********************************************************************************************/ |
16 | 17 | // 例外ベースクラス |
@@ -159,7 +160,6 @@ | ||
159 | 160 | }; |
160 | 161 | |
161 | 162 | |
162 | -#if 0 | |
163 | 163 | /*********************************************************************************************/ |
164 | 164 | // 接続スレッド用例外クラス |
165 | 165 | /*********************************************************************************************/ |
@@ -172,7 +172,7 @@ | ||
172 | 172 | CConnectionErrorException() {}; |
173 | 173 | CConnectionErrorException(CUTF8String errorInfo) |
174 | 174 | { |
175 | - SetCaller(""); | |
175 | + SetCaller(_U8("")); | |
176 | 176 | SetErrorInfo(_U16("%s"), errorInfo); |
177 | 177 | TRACE_U8(_U16("CConnectionErrorException: %s\n"), errorInfo); |
178 | 178 | } |
@@ -198,7 +198,7 @@ | ||
198 | 198 | { |
199 | 199 | public: |
200 | 200 | CServerResponseException() {}; |
201 | - CServerResponseException(int responseCode, CUTF8String status, CUTF8String text = "") | |
201 | + CServerResponseException(int responseCode, CUTF8String status, CUTF8String text = _U8("")) | |
202 | 202 | { |
203 | 203 | m_responseCode = responseCode; |
204 | 204 | m_status = status; |
@@ -238,7 +238,7 @@ | ||
238 | 238 | } |
239 | 239 | |
240 | 240 | //! 追加されるヘッダ |
241 | - CKeyDataArray &GetAdditionalHeader() | |
241 | + CUTF8StringKeyPairArray &GetAdditionalHeader() | |
242 | 242 | { |
243 | 243 | return m_additionalHeader; |
244 | 244 | } |
@@ -248,16 +248,14 @@ | ||
248 | 248 | // メンバ変数 |
249 | 249 | ////////////////////////////////////////////////////////////// |
250 | 250 | //! レスポンスコード |
251 | - int m_responseCode; | |
251 | + int m_responseCode; | |
252 | 252 | |
253 | 253 | //! レスポンス情報(1行) |
254 | - CUTF8String m_status; | |
254 | + CUTF8String m_status; | |
255 | 255 | |
256 | 256 | //! 詳細(複数行、CGIエラー詳細など) |
257 | - CUTF8String m_text; | |
257 | + CUTF8String m_text; | |
258 | 258 | |
259 | 259 | //! 追加されるヘッダ |
260 | - CKeyDataArray m_additionalHeader; | |
260 | + CUTF8StringKeyPairArray m_additionalHeader; | |
261 | 261 | }; |
262 | - | |
263 | -#endif | |
\ No newline at end of file |
@@ -0,0 +1,14 @@ | ||
1 | +#pragma once | |
2 | + | |
3 | + | |
4 | +class CUrlUtility | |
5 | +{ | |
6 | +public: | |
7 | + static CString GetQuery(CString url); | |
8 | + static CString GetUrlObject(CString url); | |
9 | + static CString DecodeUrl(CString url); | |
10 | + static CString Decode(CString url); | |
11 | + static CString GetDirectoryPath(CString serverPath); | |
12 | + static CString EncodeUrl(CString Url); | |
13 | + static CString EncodeUrl8Bit(CString Url); | |
14 | +}; |
@@ -0,0 +1,63 @@ | ||
1 | +/*! | |
2 | + CNormalListener | |
3 | + | |
4 | + IPv4用の接続受付クラス | |
5 | +*/ | |
6 | + | |
7 | +#pragma once | |
8 | +#include "ThreadSkeleton.h" | |
9 | +#include "Settings.h" | |
10 | +#include "IPv4ListenerBase.h" | |
11 | +#include "IPv4ClientConnection.h" | |
12 | + | |
13 | + | |
14 | +/*! | |
15 | + ノーマルリスナ | |
16 | +*/ | |
17 | +class CNormalListener: | |
18 | + public IListener, protected CIPv4ListenerBase | |
19 | +{ | |
20 | +public: | |
21 | + CNormalListener(void); | |
22 | + virtual ~CNormalListener(void); | |
23 | + | |
24 | + ///////////////////////////////////////////////////////////////////////////////////// | |
25 | + // 操作 | |
26 | + ///////////////////////////////////////////////////////////////////////////////////// | |
27 | + //! 開始 | |
28 | + virtual void Start(IServerToolPtr serverTool, IClientConnectionQueuePtr queue); | |
29 | + | |
30 | + //! 停止 | |
31 | + virtual void Stop(); | |
32 | + | |
33 | + //! 停止準備 | |
34 | + virtual void ShutdownListen(); | |
35 | + | |
36 | +protected: | |
37 | + ///////////////////////////////////////////////////////////////////////////////////// | |
38 | + // オーバーロード | |
39 | + ///////////////////////////////////////////////////////////////////////////////////// | |
40 | + //! クラス名取得 | |
41 | + virtual LPCU8STR GetListenerClassName(); | |
42 | + | |
43 | + //! 接続追加 | |
44 | + virtual void OnNewConnection(SOCKET newSock); | |
45 | + | |
46 | + ///////////////////////////////////////////////////////////////////////////////////// | |
47 | + // メンバ変数 | |
48 | + ///////////////////////////////////////////////////////////////////////////////////// | |
49 | + //! 初期化フラグ | |
50 | + CInterlockCounter m_init; | |
51 | + | |
52 | + //! サーバツール | |
53 | + CIServerToolWrap m_serverTool; | |
54 | + | |
55 | + //! 設定 | |
56 | + CIPv4ListenerSetting m_setting; | |
57 | + | |
58 | + //! キュー | |
59 | + IClientConnectionQueuePtr m_queue; | |
60 | + | |
61 | + //! タイムアウト時間 | |
62 | + int m_timeoutTime; | |
63 | +}; |
@@ -0,0 +1,151 @@ | ||
1 | +#include "StdAfx.h" | |
2 | +#include "IPv4ClientConnection.h" | |
3 | +#include "Context.h" | |
4 | +#include "ServerException.h" | |
5 | + | |
6 | +#define MAX_HEADER_SIZE (16 * 1024) // 16K | |
7 | +#define END_HEADER _U8("\r\n\r\n") | |
8 | + | |
9 | + | |
10 | +////////////////////////////////////////////////////////////////////////////////////////////// | |
11 | +// コンストラクタ | |
12 | +////////////////////////////////////////////////////////////////////////////////////////////// | |
13 | +/*! | |
14 | + コンストラクタ | |
15 | +*/ | |
16 | +CIPv4ClientConnection::CIPv4ClientConnection(SOCKET sock, int timeout) | |
17 | +{ | |
18 | + m_socket.AttachSocket(sock); | |
19 | + m_socket.SetTimeout(timeout); | |
20 | +} | |
21 | + | |
22 | + | |
23 | +/*! | |
24 | + デストラクタ | |
25 | +*/ | |
26 | +CIPv4ClientConnection::~CIPv4ClientConnection(void) | |
27 | +{ | |
28 | + Close(); | |
29 | +} | |
30 | + | |
31 | + | |
32 | +///////////////////////////////////////////////////////////////////// | |
33 | +// IClientConnection | |
34 | +///////////////////////////////////////////////////////////////////// | |
35 | +/*! | |
36 | + 初期化 | |
37 | +*/ | |
38 | +void CIPv4ClientConnection::AttacheToThread(IServerToolPtr serverTool, IConnectionThreadStatusPtr threadStatus) | |
39 | +{ | |
40 | + m_threadStatus = threadStatus; | |
41 | + m_socket.SetExBreakEvent(m_threadStatus->GetBreakEvent()); | |
42 | +} | |
43 | + | |
44 | +/*! | |
45 | + 切断 | |
46 | +*/ | |
47 | +void CIPv4ClientConnection::Close() | |
48 | +{ | |
49 | + m_socket.Close(); | |
50 | +} | |
51 | + | |
52 | + | |
53 | +/*! | |
54 | + クライアント情報の取得 | |
55 | +*/ | |
56 | +void CIPv4ClientConnection::GetClientInfo(IWorkspaceAccessPtr clientInfo) | |
57 | +{ | |
58 | + // ラッパ定義 | |
59 | + CContextClientInfo info = CWorkspaceAccess(clientInfo); | |
60 | + | |
61 | + // クライアント情報 | |
62 | + // ホスト名は自動設定されない | |
63 | + info.SetAcceptedPort(m_socket.GetPort()); | |
64 | + info.SetAcceptedAddress(m_socket.GetAddres()); | |
65 | + info.SetClientPort(m_socket.GetPeerPort()); | |
66 | + info.SetClientAddress(m_socket.GetPeerAddres()); | |
67 | +} | |
68 | + | |
69 | +/*! | |
70 | + 次の接続を待つ | |
71 | + | |
72 | + \return TRUE->検出、FALSE->タイムアウト | |
73 | +*/ | |
74 | +int CIPv4ClientConnection::WaitNextRequest(int waitTime) | |
75 | +{ | |
76 | + return m_socket.WaitNextRequest(waitTime); | |
77 | +} | |
78 | + | |
79 | +/*! | |
80 | + 接続中か取得する | |
81 | +*/ | |
82 | +int CIPv4ClientConnection::IsConnecting() | |
83 | +{ | |
84 | + return !m_socket.IsDisconnect(); | |
85 | +} | |
86 | + | |
87 | + | |
88 | +///////////////////////////////////////////////////////////////////// | |
89 | +// IClientConnection | |
90 | +///////////////////////////////////////////////////////////////////// | |
91 | +/*! | |
92 | + ヘッダを受信(throw) | |
93 | +*/ | |
94 | +void CIPv4ClientConnection::RecvRequestHeader(IStringPtr _requestHeader) | |
95 | +{ | |
96 | + CUTF8String requestHeader; | |
97 | + | |
98 | + // ヘッダ受信 | |
99 | + if(m_socket.RecvProtocolHeader(requestHeader, END_HEADER, MAX_HEADER_SIZE, FALSE)) | |
100 | + throw CConnectionErrorException(_U16("HTTPヘッダの受信に失敗しました: ") + m_socket.GetLastErrorString()); | |
101 | + | |
102 | + // 設定 | |
103 | + _requestHeader->SetString(requestHeader); | |
104 | +} | |
105 | + | |
106 | + | |
107 | +/*! | |
108 | + ボディを受信(throw) | |
109 | +*/ | |
110 | +int CIPv4ClientConnection::RecvRequestBody(IBinaryDataPtr _body, int leftSize) | |
111 | +{ | |
112 | + // ラッパ定義 | |
113 | + CBinaryData body(_body); | |
114 | + | |
115 | + // 受信 | |
116 | + int recved = m_socket.RecvBinary(body, leftSize); | |
117 | + if(m_socket.GetLastError()) | |
118 | + throw CConnectionErrorException(_U16("データの受信に失敗しました: ") + m_socket.GetLastErrorString()); | |
119 | + | |
120 | + return(recved); | |
121 | +} | |
122 | + | |
123 | + | |
124 | +/*! | |
125 | + ヘッダを送信(throw) | |
126 | +*/ | |
127 | +void CIPv4ClientConnection::SendResponseHeader(IStringPtr _responseHeader) | |
128 | +{ | |
129 | + CUTF8String responseHeader = _responseHeader->GetString(); | |
130 | + | |
131 | + // レスポンスヘッダ送信 | |
132 | + m_socket.SendString(responseHeader); | |
133 | + if(m_socket.GetLastError()) | |
134 | + throw CConnectionErrorException(_U16("HTTPヘッダの送信に失敗しました: ") + m_socket.GetLastErrorString()); | |
135 | +} | |
136 | + | |
137 | +/*! | |
138 | + ボディを送信(throw) | |
139 | +*/ | |
140 | +void CIPv4ClientConnection::SendResponseBody(IBinaryDataPtr _body) | |
141 | +{ | |
142 | + // ラッパ定義 | |
143 | + CBinaryData body(_body); | |
144 | + | |
145 | + // 送信 | |
146 | + m_socket.SendBinary(body); | |
147 | + if(m_socket.GetLastError()) | |
148 | + throw CConnectionErrorException(_U16("データの送信に失敗しました: ") + m_socket.GetLastErrorString()); | |
149 | +} | |
150 | + | |
151 | + |
@@ -0,0 +1,15 @@ | ||
1 | +#pragma once | |
2 | +#include "Context.h" | |
3 | + | |
4 | +class CHTTPHeaderParser | |
5 | +{ | |
6 | +public: | |
7 | + //! ヘッダ解析 | |
8 | + static int ParseRequestHeader(CUTF8String &header, CContextRequestInfo requestInfo); | |
9 | + | |
10 | + //! ヘッダ生成 | |
11 | + static CUTF8String GetResponseHeader(CContextResponseInfo responseInfo); | |
12 | + | |
13 | + //! レスポンステク氏と取得 | |
14 | + static CUTF8String GetResponseText(int code, CUTF8String responseText = _U8("")); | |
15 | +}; |
@@ -0,0 +1,235 @@ | ||
1 | +/*! | |
2 | + 2006/06/30 脆弱性:IPA#27428836 Fix | |
3 | +*/ | |
4 | + | |
5 | +#include "stdafx.h" | |
6 | +#include "UrlUtility.h" | |
7 | +#include "JISUtility.h" | |
8 | +#include "Dir.h" | |
9 | + | |
10 | +#ifdef _DEBUG | |
11 | +#undef THIS_FILE | |
12 | +static char THIS_FILE[]=__FILE__; | |
13 | +#define new DEBUG_NEW | |
14 | +#endif | |
15 | + | |
16 | +////////////////////////////////////////////////////////////////////// | |
17 | +// 構築/消滅 | |
18 | +////////////////////////////////////////////////////////////////////// | |
19 | + | |
20 | +CUrlUtility::CUrlUtility() | |
21 | +{ | |
22 | + | |
23 | +} | |
24 | + | |
25 | +CUrlUtility::~CUrlUtility() | |
26 | +{ | |
27 | + | |
28 | +} | |
29 | + | |
30 | + | |
31 | +// サーバパスをディレクトリパスに変換 | |
32 | +CString CUrlUtility::GetDirectoryPath(CString serverPath) | |
33 | +{ | |
34 | + if(CPathTools::CheckSlash(serverPath)) | |
35 | + return(serverPath); | |
36 | + | |
37 | + int index = serverPath.ReverseFind('/'); | |
38 | + if(index == -1) | |
39 | + return("/"); | |
40 | + return(serverPath.Left(index + 1)); | |
41 | +} | |
42 | + | |
43 | + | |
44 | +// URLのデコード(展開のみ) | |
45 | +CString CUrlUtility::Decode(CString url) | |
46 | +{ | |
47 | + // 展開 | |
48 | + CString ret; | |
49 | + int i,binaryFlag = 0,len = url.GetLength(); | |
50 | + char hex[4],*e; | |
51 | + unsigned char add; | |
52 | + | |
53 | + for(i=0;i<len;i++) | |
54 | + { | |
55 | + // 日本語その他? | |
56 | + if(url[i] & 0x80) | |
57 | + binaryFlag = 1; | |
58 | + | |
59 | + if(url[i]=='%' && i < len - 2) | |
60 | + { | |
61 | + hex[0] = url[i+1]; | |
62 | + hex[1] = url[i+2]; | |
63 | + hex[2] = 0; | |
64 | + | |
65 | + add = (unsigned char)strtol(hex,&e,16); | |
66 | + if(add != 0) | |
67 | + { | |
68 | + ret += add; | |
69 | + i+=2; | |
70 | + if(add >= 0x80) | |
71 | + binaryFlag = 1; | |
72 | + } | |
73 | + } | |
74 | + else if(url[i]=='+') | |
75 | + ret += ' '; | |
76 | + else | |
77 | + ret += url[i]; | |
78 | + | |
79 | + } | |
80 | + | |
81 | + if(binaryFlag) | |
82 | + ret = CJISUtility::ToSjisString(ret); | |
83 | + | |
84 | + return(ret); | |
85 | +} | |
86 | + | |
87 | +// URLのデコード | |
88 | +CString CUrlUtility::DecodeUrl(CString url) | |
89 | +{ | |
90 | + // 前処理 | |
91 | + url.TrimLeft(); | |
92 | + url.TrimRight(); | |
93 | + | |
94 | + // 展開 | |
95 | + CString ret; | |
96 | + int i,binaryFlag = 0,len = url.GetLength(); | |
97 | + char hex[4],*e; | |
98 | + unsigned char add; | |
99 | + | |
100 | + for(i=0;i<len;i++) | |
101 | + { | |
102 | + // 日本語その他? | |
103 | + if(url[i] & 0x80) | |
104 | + binaryFlag = 1; | |
105 | + | |
106 | + if(url[i]=='%' && i < len - 2) | |
107 | + { | |
108 | + hex[0] = url[i+1]; | |
109 | + hex[1] = url[i+2]; | |
110 | + hex[2] = 0; | |
111 | + | |
112 | + add = (unsigned char)strtol(hex,&e,16); | |
113 | + if(add != 0) | |
114 | + { | |
115 | + ret += add; | |
116 | + i+=2; | |
117 | + if(add >= 0x80) | |
118 | + binaryFlag = 1; | |
119 | + } | |
120 | + } | |
121 | +// else if(url[i]=='+') | |
122 | +// ret += ' '; | |
123 | + else | |
124 | + ret += url[i]; | |
125 | + | |
126 | + } | |
127 | + | |
128 | + if(binaryFlag) | |
129 | + ret = CJISUtility::ToSjisString(ret); | |
130 | + | |
131 | + // 必ず"/"で始まる | |
132 | + if(ret.Left(1) != '/') | |
133 | + ret = '/' + ret; | |
134 | + | |
135 | + // 安全の為、\:..を排除 | |
136 | + int count = ret.GetLength(); | |
137 | + for(i=0;i<count;i++) | |
138 | + { | |
139 | + int rep = 0; | |
140 | + | |
141 | + rep += ret.Replace('\\','/'); | |
142 | + rep += ret.Replace(':','/'); | |
143 | + rep += ret.Replace("/./","/"); // 脆弱性:IPA#27428836 2006/06/30 Fix | |
144 | + rep += ret.Replace("/../","/"); | |
145 | + rep += ret.Replace("//","/"); | |
146 | + if(rep == 0) | |
147 | + break; | |
148 | + } | |
149 | + | |
150 | + return(ret); | |
151 | +} | |
152 | + | |
153 | +// URLから、パス以外を削除 | |
154 | +CString CUrlUtility::GetUrlObject(CString url) | |
155 | +{ | |
156 | + int index = url.Find("?"); | |
157 | + if(index == -1) | |
158 | + return(url); | |
159 | + else | |
160 | + return(url.Left(index)); | |
161 | +} | |
162 | + | |
163 | +// 要求 | |
164 | +CString CUrlUtility::GetQuery(CString url) | |
165 | +{ | |
166 | + int index = url.Find("?"); | |
167 | + if(index == -1) | |
168 | + return(""); | |
169 | + else | |
170 | + return(url.Mid(index + 1)); | |
171 | +} | |
172 | + | |
173 | +// URLのエンコード | |
174 | +CString CUrlUtility::EncodeUrl(CString Url) | |
175 | +{ | |
176 | + int i,len = Url.GetLength(),j=0; | |
177 | + CString ret; | |
178 | + unsigned char *inUrl = (unsigned char*)Url.GetBuffer(0),*outUrl = (unsigned char*)ret.GetBuffer(Url.GetLength() * 3); | |
179 | + | |
180 | + | |
181 | + for(i=0;i<len;i++) | |
182 | + { | |
183 | + if(inUrl[i] == '%') | |
184 | + TRACE("Test"); | |
185 | + if(inUrl[i] <= 0x20 || inUrl[i] == '%' || inUrl[i] == '#') | |
186 | + { | |
187 | + // 危険な文字 | |
188 | + char tmp[10]; | |
189 | + | |
190 | + sprintf_s(tmp, 10, "%02x", inUrl[i]); | |
191 | + outUrl[j++]='%'; | |
192 | + outUrl[j++]=tmp[0]; | |
193 | + outUrl[j++]=tmp[1]; | |
194 | + } | |
195 | + else | |
196 | + { | |
197 | + outUrl[j++]=inUrl[i]; | |
198 | + } | |
199 | + } | |
200 | + outUrl[j]=0; | |
201 | + | |
202 | + ret.ReleaseBuffer(); | |
203 | + return(ret); | |
204 | +} | |
205 | + | |
206 | +// URLのエンコード(8ビット以上の値もエスケイプされる) | |
207 | +CString CUrlUtility::EncodeUrl8Bit(CString Url) | |
208 | +{ | |
209 | + int i,len = Url.GetLength(),j=0; | |
210 | + CString ret; | |
211 | + unsigned char *inUrl = (unsigned char*)Url.GetBuffer(0),*outUrl = (unsigned char*)ret.GetBuffer(Url.GetLength() * 3); | |
212 | + | |
213 | + | |
214 | + for(i=0;i<len;i++) | |
215 | + { | |
216 | + if(inUrl[i] <= 0x20 || inUrl[i] == '%' || inUrl[i] == '#' || inUrl[i] >= 0x80) | |
217 | + { | |
218 | + // 危険な文字 | |
219 | + char tmp[10]; | |
220 | + | |
221 | + sprintf_s(tmp, 10, "%02x", inUrl[i]); | |
222 | + outUrl[j++]='%'; | |
223 | + outUrl[j++]=tmp[0]; | |
224 | + outUrl[j++]=tmp[1]; | |
225 | + } | |
226 | + else | |
227 | + { | |
228 | + outUrl[j++]=inUrl[i]; | |
229 | + } | |
230 | + } | |
231 | + outUrl[j]=0; | |
232 | + | |
233 | + ret.ReleaseBuffer(); | |
234 | + return(ret); | |
235 | +} |
@@ -0,0 +1,104 @@ | ||
1 | +#include "StdAfx.h" | |
2 | +#include "IPv4Listener.h" | |
3 | +#include "ServerException.h" | |
4 | + | |
5 | +#define CLASS_NAME _U8("NormalListener") | |
6 | + | |
7 | + | |
8 | +CNormalListener::CNormalListener(void) | |
9 | +{ | |
10 | + m_init = 0; | |
11 | +} | |
12 | + | |
13 | +CNormalListener::~CNormalListener(void) | |
14 | +{ | |
15 | + Stop(); | |
16 | +} | |
17 | + | |
18 | + | |
19 | + | |
20 | +/*! | |
21 | + 開始 | |
22 | +*/ | |
23 | +void CNormalListener::Start(IServerToolPtr serverTool, IClientConnectionQueuePtr queue) | |
24 | +{ | |
25 | + // 初期化済み? | |
26 | + if(m_init) | |
27 | + return; | |
28 | + m_init = TRUE; | |
29 | + | |
30 | + // コピー | |
31 | + m_serverTool = serverTool; | |
32 | + m_queue = queue; | |
33 | + m_setting = m_serverTool.GetManagerSetting(m_setting.GetSettingName()); | |
34 | + | |
35 | + // 設定取得 | |
36 | + m_timeoutTime = m_setting.GetConnectionTimeoutTime(); | |
37 | + | |
38 | + // リスナ初期化 | |
39 | + InitListenerBase(m_serverTool.GetIServerTool(), m_setting.GetListenErrorRetryTime()); | |
40 | + | |
41 | + // ポート追加 | |
42 | + AddPortsFromSetting(m_setting.GetPort()); | |
43 | + | |
44 | + // ポート数確認 | |
45 | + if(GetPortCount() == 0) | |
46 | + throw CServerStartErrorException(CLASS_NAME, _U16("待ち受けポートがひとつも指定されていません")); | |
47 | + | |
48 | + // スレッド起動 | |
49 | + StartListenerBase(); | |
50 | +} | |
51 | + | |
52 | +/*! | |
53 | + 停止 | |
54 | +*/ | |
55 | +void CNormalListener::Stop() | |
56 | +{ | |
57 | + // 初期化チェック | |
58 | + if(!m_init) | |
59 | + return; | |
60 | + m_init = FALSE; | |
61 | + | |
62 | + StopListenerBase(); | |
63 | +} | |
64 | + | |
65 | +/*! | |
66 | + 停止準備 | |
67 | +*/ | |
68 | +void CNormalListener::ShutdownListen() | |
69 | +{ | |
70 | + Stop(); // このクラスでは同じ | |
71 | +} | |
72 | + | |
73 | +///////////////////////////////////////////////////////////////////////////////////// | |
74 | +// オーバーロード | |
75 | +///////////////////////////////////////////////////////////////////////////////////// | |
76 | +/*! | |
77 | + クラス名取得 | |
78 | +*/ | |
79 | +LPCU8STR CNormalListener::GetListenerClassName() | |
80 | +{ | |
81 | + return CLASS_NAME; | |
82 | +} | |
83 | + | |
84 | +/*! | |
85 | + 接続追加 | |
86 | +*/ | |
87 | +void CNormalListener::OnNewConnection(SOCKET newSock) | |
88 | +{ | |
89 | + try | |
90 | + { | |
91 | + m_queue->AddNewConnection(new CIPv4ClientConnection(newSock, m_timeoutTime)); | |
92 | + } | |
93 | + catch(CBadAllocException &e) | |
94 | + { | |
95 | + // 使わないので... | |
96 | + UNREFERENCED_PARAMETER(e); | |
97 | + | |
98 | + // ログへ | |
99 | + m_serverTool.WriteSystemLog(GetListenerClassName(), SYSTEM_LOG_ERROR, _U16("メモリ不足が発生しました、一時的に接続を拒否します")); | |
100 | + | |
101 | + // メモリ不足のため、サーバ一時停止 | |
102 | + ForceDisableNewConnection(30 * 1000); | |
103 | + } | |
104 | +} |
@@ -0,0 +1,100 @@ | ||
1 | +/*! | |
2 | + CIPv4ListenerBase | |
3 | + | |
4 | + IPv4用系接続受付クラスの基本クラス | |
5 | +*/ | |
6 | + | |
7 | +#pragma once | |
8 | +#include "ThreadSkeleton.h" | |
9 | +#include "ListenSocket.h" | |
10 | +#include "IListener.h" | |
11 | +#include "IServerToolWrap.h" | |
12 | +#include "Settings.h" | |
13 | + | |
14 | +/*! | |
15 | + IPv4用系接続受付クラスの基本クラス | |
16 | +*/ | |
17 | +class CIPv4ListenerBase : | |
18 | + private CThreadSkeleton | |
19 | +{ | |
20 | +public: | |
21 | + CIPv4ListenerBase(void); | |
22 | + virtual ~CIPv4ListenerBase(void); | |
23 | + | |
24 | +protected: | |
25 | + ///////////////////////////////////////////////////////////////////////////////////// | |
26 | + // オーバーロード | |
27 | + ///////////////////////////////////////////////////////////////////////////////////// | |
28 | + //! クラス名取得 | |
29 | + virtual LPCU8STR GetListenerClassName() = 0; | |
30 | + | |
31 | + //! 接続追加 | |
32 | + virtual void OnNewConnection(SOCKET newSock) = 0; | |
33 | + | |
34 | + ///////////////////////////////////////////////////////////////////////////////////// | |
35 | + // 操作 | |
36 | + ///////////////////////////////////////////////////////////////////////////////////// | |
37 | + //! 初期化 | |
38 | + void InitListenerBase(CIServerToolWrap serverTool, int errorRetryTime); | |
39 | + | |
40 | + //! 開始 | |
41 | + void StartListenerBase(); | |
42 | + | |
43 | + //! 停止 | |
44 | + void StopListenerBase(); | |
45 | + | |
46 | + //! 設定からポート追加 | |
47 | + void AddPortsFromSetting(CUTF8String strPorts); | |
48 | + | |
49 | + //! ポートの追加 | |
50 | + void AddPort(int port); | |
51 | + | |
52 | + //! ポート数取得 | |
53 | + int GetPortCount() { return m_listeners.GetCount(); }; | |
54 | + | |
55 | + //! 一時的に受け付け停止 | |
56 | + void ForceDisableNewConnection(int time); | |
57 | + | |
58 | +private: | |
59 | + ///////////////////////////////////////////////////////////////////////////////////// | |
60 | + // スレッド関数 | |
61 | + ///////////////////////////////////////////////////////////////////////////////////// | |
62 | + //! スレッド | |
63 | + void ThreadMain(); | |
64 | + | |
65 | + //! クリーンアップ | |
66 | + void CloseAllPorts(); | |
67 | + | |
68 | + //! 受付ループ | |
69 | + int AcceptLoop(); | |
70 | + | |
71 | + //! ポートを開く | |
72 | + int OpenPorts(); | |
73 | + | |
74 | + //! ポートを閉じる | |
75 | + void ClosePorts(); | |
76 | + | |
77 | + //! 接続を受け付け | |
78 | + int AcceptConnections(); | |
79 | + | |
80 | + //! 接続が来るのを待つ | |
81 | + void WaitNextRequest(); | |
82 | + | |
83 | + //! 停止中? | |
84 | + int InDisableMode(); | |
85 | + | |
86 | + ///////////////////////////////////////////////////////////////////////////////////// | |
87 | + // メンバ変数 | |
88 | + ///////////////////////////////////////////////////////////////////////////////////// | |
89 | + //! サーバツール | |
90 | + CIServerToolWrap m_serverTool; | |
91 | + | |
92 | + //! リスナ配列 | |
93 | + CPtrArrayTemplate<CListenSocket*> m_listeners; | |
94 | + | |
95 | + //! エラーリトライ時間 | |
96 | + int m_errorRetryTime; | |
97 | + | |
98 | + //! 受付無効終了予定 | |
99 | + DWORD m_disableNewConnectionEndTime; | |
100 | +}; |