1 #include "NyLPC_cHttpdConnection_protected.h"
\r
2 #include "../http/NyLPC_cHttpNullRequestHeaderParser.h"
\r
3 #include "./mod/NyLPC_cHttpModUtils_protected.h"
\r
4 #include "NyLPC_cHttpd.h"
\r
8 void NyLPC_cHttpdConnection_initialize(NyLPC_TcHttpdConnection_t* i_inst,NyLPC_TcHttpd_t* i_parent_httpd)
\r
10 NyLPC_cTcpSocket_initialize(&(i_inst->_socket),i_inst->_rxbuf,NyLPC_NyLPC_TcHttpdConnection_SIZE_OF_RX_BUF);
\r
11 NyLPC_cHttpRequestPrefixParser_initialize(&(i_inst->_pparser));
\r
12 i_inst->_parent_httpd=i_parent_httpd;
\r
13 i_inst->_res_status=NyLPC_cHttpdConnection_ResStatus_CLOSED;
\r
14 i_inst->_req_status=NyLPC_cHttpdConnection_ReqStatus_LISTEN;
\r
17 void NyLPC_cHttpdConnection_finalize(NyLPC_TcHttpdConnection_t* i_inst)
\r
19 NyLPC_cHttpdConnection_closeResponse(i_inst);
\r
20 NyLPC_cHttpdConnection_closeSocket(i_inst);
\r
21 NyLPC_cHttpRequestPrefixParser_finalize(i_inst);
\r
22 NyLPC_cTcpSocket_finalize(&(i_inst->_socket));
\r
25 const NyLPC_TChar* NyLPC_cHttpdConnection_getUrlPrefix(const NyLPC_TcHttpdConnection_t* i_inst)
\r
27 return NyLPC_cHttpRequestPrefixParser_getUrlPrefix(&i_inst->_pparser);
\r
30 #define NyLPC_cHttpHeaderWriter_CONTENT_LENGTH_UNLIMITED 0xFFFFFFFF
\r
34 NyLPC_TBool NyLPC_cHttpdConnection_sendResponseHeader(NyLPC_TcHttpdConnection_t* i_inst,NyLPC_TUInt16 i_response_code,const NyLPC_TChar* i_content_type,const NyLPC_TChar* i_additional_header)
\r
36 return NyLPC_cHttpdConnection_sendResponseHeader2(i_inst,i_response_code,i_content_type,NyLPC_cHttpHeaderWriter_CONTENT_LENGTH_UNLIMITED,i_additional_header);
\r
39 NyLPC_TBool NyLPC_cHttpdConnection_sendResponseHeader2(NyLPC_TcHttpdConnection_t* i_inst,NyLPC_TUInt16 i_response_code,const NyLPC_TChar* i_content_type,NyLPC_TUInt32 i_content_length,const NyLPC_TChar* i_additional_header)
\r
41 NyLPC_TcHttpHeaderWriter_t* h=&(i_inst->_head_writer);
\r
43 if(i_inst->_res_status!=NyLPC_cHttpdConnection_ResStatus_HEAD)
\r
45 NyLPC_OnErrorGoto(Error_Status);
\r
48 if(!NyLPC_cHttpHeaderWriter_initialize(h,&(i_inst->_in_stream),NULL)){
\r
49 NyLPC_OnErrorGoto(ERROR_SEND);
\r
52 if(i_content_length==NyLPC_cHttpHeaderWriter_CONTENT_LENGTH_UNLIMITED){
\r
53 NyLPC_cHttpHeaderWriter_setChunked(h);
\r
55 NyLPC_cHttpHeaderWriter_setContentLength(h,i_content_length);
\r
57 //closeがセットされていればclose
\r
58 if(i_inst->_connection_message_mode==NyLPC_TcHttpdConnection_CONNECTION_MODE_CLOSE)
\r
60 NyLPC_cHttpHeaderWriter_setClose(h);
\r
63 if(!NyLPC_cHttpHeaderWriter_writeHeader(h,i_response_code)){
\r
64 NyLPC_OnErrorGoto(ERROR_SEND);
\r
66 if(!NyLPC_cHttpHeaderWriter_writeMessage(h,"Content-type",i_content_type)){
\r
67 NyLPC_OnErrorGoto(ERROR_SEND);
\r
69 if(i_additional_header!=NULL){
\r
70 if(!NyLPC_cHttpHeaderWriter_writeRawMessage(h,i_additional_header)){
\r
71 NyLPC_OnErrorGoto(ERROR_SEND);
\r
74 NyLPC_cHttpHeaderWriter_close(h);
\r
75 NyLPC_cHttpHeaderWriter_finalize(h);
\r
76 i_inst->_res_status=NyLPC_cHttpdConnection_ResStatus_BODY;
\r
78 NyLPC_cHttpBodyWriter_initialize(&(i_inst->_body_writer),&(i_inst->_in_stream));
\r
80 if(i_content_length==NyLPC_cHttpHeaderWriter_CONTENT_LENGTH_UNLIMITED){
\r
81 NyLPC_cHttpBodyWriter_setChunked(&(i_inst->_body_writer));
\r
83 NyLPC_cHttpBodyWriter_setContentLength(&(i_inst->_body_writer),i_content_length);
\r
85 return NyLPC_TBool_TRUE;
\r
87 NyLPC_cHttpHeaderWriter_finalize(&(i_inst->_head_writer));
\r
89 i_inst->_res_status=NyLPC_cHttpdConnection_ResStatus_ERROR;
\r
90 return NyLPC_TBool_FALSE;
\r
96 * 関数を実行後、_res_statusはBODYかERRORに遷移します。
\r
98 NyLPC_TBool NyLPC_cHttpdConnection_sendResponseBody(NyLPC_TcHttpdConnection_t* i_inst,const void* i_data,NyLPC_TUInt32 i_size)
\r
100 if(i_inst->_res_status!=NyLPC_cHttpdConnection_ResStatus_BODY)
\r
102 NyLPC_OnErrorGoto(Error);
\r
105 if(!NyLPC_cHttpBodyWriter_write(&(i_inst->_body_writer),i_data,i_size)){
\r
106 NyLPC_OnErrorGoto(Error_Send);
\r
108 return NyLPC_TBool_TRUE;
\r
110 NyLPC_cHttpBodyWriter_finalize(&(i_inst->_in_stream));
\r
112 i_inst->_res_status=NyLPC_cHttpdConnection_ResStatus_ERROR;
\r
113 return NyLPC_TBool_FALSE;
\r
116 * レスポンスBodyを書式出力して送信します。
\r
117 * 関数を実行後、_res_statusはBODYかERRORに遷移します。
\r
119 NyLPC_TBool NyLPC_cHttpdConnection_sendResponseBodyF(NyLPC_TcHttpdConnection_t* i_inst,const char* i_fmt,...)
\r
122 if(i_inst->_res_status!=NyLPC_cHttpdConnection_ResStatus_BODY)
\r
124 NyLPC_OnErrorGoto(Error);
\r
128 if(!NyLPC_cHttpBodyWriter_formatV(&(i_inst->_body_writer),i_fmt,a)){
\r
129 NyLPC_OnErrorGoto(Error_Send);
\r
132 return NyLPC_TBool_TRUE;
\r
135 NyLPC_cHttpBodyWriter_finalize(&(i_inst->_in_stream));
\r
137 i_inst->_res_status=NyLPC_cHttpdConnection_ResStatus_ERROR;
\r
138 return NyLPC_TBool_FALSE;
\r
142 * ヘッダのみのErrorレスポンスを送信する。
\r
143 * この関数はワーク用のHeaderWriterを使います。
\r
145 static void sendErrorResponse(NyLPC_TcHttpdConnection_t* i_inst,NyLPC_TInt16 i_status)
\r
147 NyLPC_TcHttpHeaderWriter_t* h=&(i_inst->_head_writer);
\r
148 if(NyLPC_cHttpHeaderWriter_initialize(h,&i_inst->_in_stream,NULL)){
\r
150 NyLPC_cHttpHeaderWriter_setClose(h);
\r
151 NyLPC_cHttpHeaderWriter_writeHeader(h,i_status);
\r
152 NyLPC_cHttpHeaderWriter_close(h);
\r
153 NyLPC_cHttpHeaderWriter_finalize(h);
\r
157 * 関数を実行後、_res_statusはCLOSEDかHEADかERRORに遷移する。
\r
159 * TRUE/FALSE TCPコネクションを切断するかどうかのフラグ
\r
161 NyLPC_TBool NyLPC_cHttpdConnection_closeResponse(NyLPC_TcHttpdConnection_t* i_inst)
\r
163 NyLPC_TcHttpBodyWriter_t* b;
\r
164 switch(i_inst->_res_status){
\r
165 case NyLPC_cHttpdConnection_ResStatus_CLOSED:
\r
166 case NyLPC_cHttpdConnection_ResStatus_ERROR:
\r
167 //何もせずにコネクションをクローズする。
\r
168 return NyLPC_TBool_FALSE;
\r
169 case NyLPC_cHttpdConnection_ResStatus_HEAD:
\r
170 //エラー500を送信してクローズする。
\r
171 sendErrorResponse(i_inst,500);
\r
172 i_inst->_res_status=NyLPC_cHttpdConnection_ResStatus_CLOSED;
\r
173 return NyLPC_TBool_FALSE;
\r
174 case NyLPC_cHttpdConnection_ResStatus_BODY:
\r
175 //正常終了。BODYをクローズし、終了する。
\r
176 b=&(i_inst->_body_writer);
\r
177 NyLPC_cHttpBodyWriter_close(b);
\r
178 NyLPC_cHttpBodyWriter_finalize(&b);
\r
179 i_inst->_res_status=NyLPC_cHttpdConnection_ResStatus_HEAD;
\r
180 if(i_inst->_connection_message_mode==NyLPC_TcHttpdConnection_CONNECTION_MODE_CLOSE)
\r
182 i_inst->_res_status=NyLPC_cHttpdConnection_ResStatus_CLOSED;
\r
183 return NyLPC_TBool_FALSE;
\r
185 return NyLPC_TBool_TRUE;
\r
189 return NyLPC_TBool_TRUE;
\r
191 void NyLPC_cHttpdConnection_setConnectionMode(NyLPC_TcHttpdConnection_t* i_inst,NyLPC_TcHttpdConnection_CONNECTION_MODE i_mode)
\r
193 i_inst->_connection_message_mode=i_mode;
\r
197 * コネクションのプリフェッチデータをヘッダパーサへpushします。
\r
199 NyLPC_TBool NyLPC_cHttpdConnection_pushPrefetchInfo(NyLPC_TcHttpdConnection_t* i_inst,NyLPC_TcHttpBasicHeaderParser_t* i_header_parser,struct NyLPC_THttpBasicHeader* o_out)
\r
201 const char* method=NyLPC_cHttpdModUtils_getMethodPrefix(i_inst->_pparser.method);
\r
202 if(NyLPC_cHttpBasicHeaderParser_parseChar(i_header_parser,method,strlen(method),o_out)<0){
\r
203 NyLPC_OnErrorGoto(Error);
\r
205 if(NyLPC_cHttpBasicHeaderParser_parseChar(i_header_parser," ",1,o_out)<0){
\r
206 NyLPC_OnErrorGoto(Error);
\r
208 if(NyLPC_cHttpBasicHeaderParser_parseChar(i_header_parser,i_inst->_pparser._url,strlen(i_inst->_pparser._url),o_out)<0){
\r
209 NyLPC_OnErrorGoto(Error);
\r
211 return NyLPC_TBool_TRUE;
\r
213 return NyLPC_TBool_FALSE;
\r
216 #define NyLPC_cHttpdConnection_TIMEOUT_ACCEPT 3000
\r
217 #define NyLPC_cHttpdConnection_TIMEOUT_CLOSE 5000
\r
218 #define NyLPC_cHttpdConnection_TIMEOUT_LISTEN 5000
\r
222 * listenerでConnectionのソケットに接続を待ちます。
\r
224 NyLPC_TBool NyLPC_cHttpdConnection_listenSocket(NyLPC_TcHttpdConnection_t* i_inst,NyLPC_TcTcpListener_t* i_listener)
\r
226 NyLPC_Assert(i_inst->_req_status==NyLPC_cHttpdConnection_ReqStatus_LISTEN);
\r
228 if(!NyLPC_cTcpListener_listen(i_listener,&(i_inst->_socket),NyLPC_cHttpdConnection_TIMEOUT_LISTEN)){
\r
229 return NyLPC_TBool_FALSE;
\r
232 i_inst->_req_status=NyLPC_cHttpdConnection_ReqStatus_ACCEPT;
\r
233 return NyLPC_TBool_TRUE;
\r
237 * コネクションのソケットをacceptします。
\r
239 NyLPC_TBool NyLPC_cHttpdConnection_acceptSocket(NyLPC_TcHttpdConnection_t* i_inst)
\r
241 NyLPC_Assert(i_inst->_req_status==NyLPC_cHttpdConnection_ReqStatus_ACCEPT);
\r
243 if(!NyLPC_cTcpSocket_accept(&(i_inst->_socket),NyLPC_cHttpdConnection_TIMEOUT_ACCEPT)){
\r
244 NyLPC_OnErrorGoto(Error);
\r
247 if(!NyLPC_cHttpStream_initialize(&i_inst->_in_stream,&(i_inst->_socket))){
\r
248 NyLPC_OnErrorGoto(Error_Connected);
\r
251 i_inst->_res_status=NyLPC_cHttpdConnection_ResStatus_HEAD;
\r
252 i_inst->_req_status=NyLPC_cHttpdConnection_ReqStatus_PREFETCH;
\r
253 i_inst->_connection_message_mode=NyLPC_TcHttpdConnection_CONNECTION_MODE_CONTINUE;
\r
254 return NyLPC_TBool_TRUE;
\r
256 NyLPC_cTcpSocket_close(&(i_inst->_socket),NyLPC_cHttpdConnection_TIMEOUT_CLOSE);
\r
257 i_inst->_req_status=NyLPC_cHttpdConnection_ReqStatus_LISTEN;
\r
259 return NyLPC_TBool_FALSE;
\r
262 NyLPC_TBool NyLPC_cHttpdConnection_prefetch(NyLPC_TcHttpdConnection_t* i_inst)
\r
264 NyLPC_Assert(i_inst->_req_status==NyLPC_cHttpdConnection_ReqStatus_PREFETCH);
\r
267 if(!NyLPC_cHttpRequestPrefixParser_parse(&i_inst->_pparser,&i_inst->_in_stream)){
\r
269 sendErrorResponse(i_inst,400);
\r
270 NyLPC_OnErrorGoto(Error_Prefetch);
\r
272 i_inst->_req_status=NyLPC_cHttpdConnection_ReqStatus_REQPARSE;
\r
273 return NyLPC_TBool_TRUE;
\r
275 NyLPC_cTcpSocket_close(&(i_inst->_socket),NyLPC_cHttpdConnection_TIMEOUT_CLOSE);
\r
276 i_inst->_req_status=NyLPC_cHttpdConnection_ReqStatus_LISTEN;
\r
277 return NyLPC_TBool_FALSE;
\r
286 NyLPC_TBool NyLPC_cHttpdConnection_prevNextPrefetch(NyLPC_TcHttpdConnection_t* i_inst)
\r
288 NyLPC_TcHttpNullRequestHeaderParser_t parser;
\r
289 switch(i_inst->_req_status)
\r
291 case NyLPC_cHttpdConnection_ReqStatus_REQPARSE:
\r
292 //リクエストパース待ちなら前段のリクエストを吸収しておく。
\r
293 NyLPC_cHttpNullRequestHeaderParser_initialize(&parser);
\r
295 NyLPC_cHttpNullRequestHeaderParser_parseInit(&parser);
\r
296 NyLPC_cHttpNullRequestHeaderParser_parseChar(&parser,"GET ",4);//決め打ち
\r
297 NyLPC_cHttpNullRequestHeaderParser_parseChar(&parser,i_inst->_pparser._url,strlen(i_inst->_pparser._url));
\r
299 if(NyLPC_cHttpNullRequestHeaderParser_parseStream(&parser,&(i_inst->_in_stream))){
\r
300 if(NyLPC_cHttpNullRequestHeaderParser_parseFinish(&parser)){
\r
301 NyLPC_cHttpNullRequestHeaderParser_finalize(&parser);
\r
303 sendErrorResponse(i_inst,403);
\r
308 NyLPC_cHttpNullRequestHeaderParser_finalize(&parser);
\r
309 //NG:400 Bad Request
\r
310 sendErrorResponse(i_inst,400);
\r
311 return NyLPC_TBool_FALSE;//吸収失敗
\r
312 case NyLPC_cHttpdConnection_ReqStatus_END:
\r
313 //リクエストがパース済みならprefetchに戻す。
\r
314 i_inst->_req_status=NyLPC_cHttpdConnection_ReqStatus_PREFETCH;
\r
319 return NyLPC_TBool_TRUE;
\r
322 void NyLPC_cHttpdConnection_closeSocket(NyLPC_TcHttpdConnection_t* i_inst)
\r
324 switch(i_inst->_req_status)
\r
326 case NyLPC_cHttpdConnection_ReqStatus_LISTEN:
\r
329 case NyLPC_cHttpdConnection_ReqStatus_REQPARSE:
\r
330 case NyLPC_cHttpdConnection_ReqStatus_PREFETCH:
\r
331 NyLPC_cHttpStream_finalize(&i_inst->_in_stream);
\r
332 case NyLPC_cHttpdConnection_ReqStatus_ACCEPT:
\r
333 NyLPC_cTcpSocket_close(&(i_inst->_socket),NyLPC_cHttpdConnection_TIMEOUT_CLOSE);
\r
337 i_inst->_req_status=NyLPC_cHttpdConnection_ReqStatus_LISTEN;
\r
338 i_inst->_res_status=NyLPC_cHttpdConnection_ResStatus_CLOSED;
\r
342 * Httpd全体で唯一のロックを取得する。
\r
344 void NyLPC_cHttpdConnection_lock(NyLPC_TcHttpdConnection_t* i_inst)
\r
346 NyLPC_cHttpd_lock((NyLPC_TcHttpd_t*)(i_inst->_parent_httpd));
\r
349 * Httpd全体で唯一のロックを開放する。
\r
351 void NyLPC_cHttpdConnection_unlock(NyLPC_TcHttpdConnection_t* i_inst)
\r
353 NyLPC_cHttpd_unlock((NyLPC_TcHttpd_t*)(i_inst->_parent_httpd));
\r