131 int handle_request(HTTPRequest *request, threadpool_t *thrpool, EventHandler *ev) { |
131 int handle_request(HTTPRequest *request, threadpool_t *thrpool, EventHandler *ev) { |
132 // handle nsapi request |
132 // handle nsapi request |
133 |
133 |
134 // create pool |
134 // create pool |
135 pool_handle_t *pool = pool_create(); |
135 pool_handle_t *pool = pool_create(); |
136 |
136 if(!pool) { |
|
137 log_ereport(LOG_FAILURE, "cannot create new memory pool for http request"); |
|
138 return 1; |
|
139 } |
|
140 |
|
141 int ret = nsapi_start_request(request, thrpool, ev, pool); |
|
142 if(ret) { |
|
143 pool_destroy(pool); |
|
144 } |
|
145 return ret; |
|
146 } |
|
147 |
|
148 int nsapi_start_request(HTTPRequest *request, threadpool_t *thrpool, EventHandler *ev, pool_handle_t *pool) { |
137 // create nsapi data structures |
149 // create nsapi data structures |
138 NSAPISession *sn = nsapisession_create(pool); |
150 NSAPISession *sn = nsapisession_create(pool); |
139 if(sn == NULL) { |
151 if(sn == NULL) { |
140 /* TODO: error */ |
152 /* TODO: error */ |
|
153 return 1; |
141 } |
154 } |
142 |
155 |
143 NSAPIRequest *rq = pool_malloc(pool, sizeof(NSAPIRequest)); |
156 NSAPIRequest *rq = pool_malloc(pool, sizeof(NSAPIRequest)); |
144 if(rq == NULL) { |
157 if(rq == NULL) { |
145 /* TODO: error */ |
158 /* TODO: error */ |
|
159 return 1; |
146 } |
160 } |
147 ZERO(rq, sizeof(NSAPIRequest)); |
161 ZERO(rq, sizeof(NSAPIRequest)); |
148 rq->rq.req_start = request->req_start; |
162 rq->rq.req_start = request->req_start; |
149 rq->phase = NSAPIAuthTrans; |
163 rq->phase = NSAPIAuthTrans; |
150 |
164 |
151 // fill session structure |
165 // fill session structure |
152 IOStream *io = NULL; |
166 IOStream *io = NULL; |
153 if(nsapisession_setconnection(sn, request->connection, request->netbuf, &io)) { |
167 if(nsapisession_setconnection(sn, request->connection, request->netbuf, &io)) { |
154 // TODO: error |
168 // TODO: error |
|
169 return 1; |
155 } |
170 } |
156 |
171 |
157 if(!ev) { |
172 if(!ev) { |
158 ev = ev_instance(get_default_event_handler()); |
173 ev = ev_instance(get_default_event_handler()); |
159 } |
174 } |
186 } |
201 } |
187 |
202 |
188 // init NSAPI request structure |
203 // init NSAPI request structure |
189 if(request_initialize(pool, request, rq) != 0) { |
204 if(request_initialize(pool, request, rq) != 0) { |
190 log_ereport(LOG_FAILURE, "Cannot initialize request structure"); |
205 log_ereport(LOG_FAILURE, "Cannot initialize request structure"); |
191 pool_destroy(pool); |
|
192 return 1; |
206 return 1; |
193 } |
207 } |
194 |
208 |
195 // set default virtual server |
209 // set default virtual server |
196 rq->vs = request->connection->listener->default_vs.vs; |
210 rq->vs = request->connection->listener->default_vs.vs; |
424 if(!strcmp(transfer_encoding, "chunked")) { |
436 if(!strcmp(transfer_encoding, "chunked")) { |
425 netbuf *nb = sn->netbuf; |
437 netbuf *nb = sn->netbuf; |
426 // a separate buffer is required for reading chunked transfer enc |
438 // a separate buffer is required for reading chunked transfer enc |
427 sn->buffer = pool_malloc(pool, nb->maxsize); |
439 sn->buffer = pool_malloc(pool, nb->maxsize); |
428 if(!sn->buffer) { |
440 if(!sn->buffer) { |
429 pool_destroy(pool); |
|
430 // TODO: error 500 |
441 // TODO: error 500 |
431 return 1; |
442 return 1; |
432 } |
443 } |
433 |
444 |
434 // copy remaining bytes from inbuf to the additional buffer |
445 // copy remaining bytes from inbuf to the additional buffer |