src/server/protocol.c

changeset 11
24d804a2799f
parent 9
30e51941a673
equal deleted inserted replaced
10:e3ae779232a9 11:24d804a2799f
31 #include "pblock.h" 31 #include "pblock.h"
32 #include "pool.h" 32 #include "pool.h"
33 #include "session.h" 33 #include "session.h"
34 #include "io.h" 34 #include "io.h"
35 35
36 36 #include "strbuf.h"
37 37
38 38
39 void protocol_status(Session *sn, Request *rq, int n, const char *m) { 39 void protocol_status(Session *sn, Request *rq, int n, const char *m) {
40 rq->status_num = n; 40 rq->status_num = n;
41 41
224 224
225 return r; 225 return r;
226 } 226 }
227 227
228 228
229 void add_http_status_line(iovec_buf_t *out, Request *rq) { 229 void add_http_status_line(sbuf_t *out, pool_handle_t *pool, Request *rq) {
230 iovec_buf_write(out, "HTTP/1.1 ", 9); 230 sbuf_write(out, "HTTP/1.1 ", 9);
231 231
232 char *status_code_str = pool_malloc(out->pool, 8); 232 char *status_code_str = pool_malloc(pool, 8);
233 int sc_len = snprintf(status_code_str, 8, "%d ", rq->status_num); 233 int sc_len = snprintf(status_code_str, 8, "%d ", rq->status_num);
234 iovec_buf_write(out, status_code_str, sc_len); 234 sbuf_write(out, status_code_str, sc_len);
235 235
236 char *scmsg = pblock_findkeyval(pb_key_status, rq->srvhdrs); 236 char *scmsg = pblock_findkeyval(pb_key_status, rq->srvhdrs);
237 iovec_buf_write(out, scmsg, strlen(scmsg)); 237 sbuf_write(out, scmsg, strlen(scmsg));
238 238
239 iovec_buf_write(out, "\r\n", 2); 239 sbuf_write(out, "\r\n", 2);
240 } 240 }
241 241
242 void add_http_response_header(iovec_buf_t *out, Request *rq) { 242 void add_http_response_header(sbuf_t *out, Request *rq) {
243 pblock *h = rq->srvhdrs; 243 pblock *h = rq->srvhdrs;
244 pb_entry *p; 244 pb_entry *p;
245 245
246 for(int i=0;i<h->hsize;i++) { 246 for(int i=0;i<h->hsize;i++) {
247 p = h->ht[i]; 247 p = h->ht[i];
261 /* make first char of name uppercase */ 261 /* make first char of name uppercase */
262 if(name[0] > 90) { 262 if(name[0] > 90) {
263 name[0] -= 32; 263 name[0] -= 32;
264 } 264 }
265 265
266 iovec_buf_write(out, name, strlen(name)); 266 sbuf_write(out, name, strlen(name));
267 iovec_buf_write(out, ": ", 2); 267 sbuf_write(out, ": ", 2);
268 iovec_buf_write(out, value, strlen(value)); 268 sbuf_write(out, value, strlen(value));
269 iovec_buf_write(out, "\r\n", 2); 269 sbuf_write(out, "\r\n", 2);
270 270
271 p = p->next; 271 p = p->next;
272 } 272 }
273 } 273 }
274 } 274 }
280 int flags; 280 int flags;
281 flags = fcntl(fd, F_GETFL, 0); 281 flags = fcntl(fd, F_GETFL, 0);
282 fcntl(fd, F_SETFL, flags ^ O_NONBLOCK); 282 fcntl(fd, F_SETFL, flags ^ O_NONBLOCK);
283 283
284 /* iovec output buffer */ 284 /* iovec output buffer */
285 iovec_buf_t *out = iovec_buf_create(sn->pool); 285 sbuf_t *out = sbuf_new(512);
286 286
287 /* add the http status line to the output buffer */ 287 /* add the http status line to the output buffer */
288 add_http_status_line(out, rq); 288 add_http_status_line(out, sn->pool, rq);
289 289
290 /* add server header */ 290 /* add server header */
291 iovec_buf_write(out, "Server: WS uap-dev\r\n", 20); 291 sbuf_write(out, "Server: WS uap-dev\r\n", 20);
292 292
293 /* add header from rq->srvhdrs */ 293 /* add header from rq->srvhdrs */
294 add_http_response_header(out, rq); 294 add_http_response_header(out, rq);
295 295
296 /* response header end */ 296 /* response header end */
297 iovec_buf_write(out, "\r\n", 2); 297 sbuf_write(out, "\r\n", 2);
298 298
299 /* flush buffer to the socket */ 299 /* flush buffer to the socket */
300 iovec_buf_flush(out, fd); 300 write(fd, out->ptr, out->length);
301 sbuf_free(out);
301 302
302 rq->senthdrs = 1; 303 rq->senthdrs = 1;
303 } 304 }
304 305
305 int request_header(char *name, char **value, Session *sn, Request *rq) { 306 int request_header(char *name, char **value, Session *sn, Request *rq) {

mercurial