# HG changeset patch # User Olaf Wintermann # Date 1325254756 -3600 # Node ID 24d804a2799f8e4e60c1013b48ba7247e99b8264 # Parent e3ae779232a9565eb3c0dcf7dc7fb2b043516ea3 Fixed response header bug diff -r e3ae779232a9 -r 24d804a2799f src/server/objs.mk --- a/src/server/objs.mk Fri Dec 30 14:06:56 2011 +0100 +++ b/src/server/objs.mk Fri Dec 30 15:19:16 2011 +0100 @@ -61,6 +61,7 @@ MOBJ += netbuf.o MOBJ += shexp.o MOBJ += objecttype.o +MOBJ += strbuf.o MAINOBJS = $(MOBJ:%=$(OBJPRE)%) diff -r e3ae779232a9 -r 24d804a2799f src/server/protocol.c --- a/src/server/protocol.c Fri Dec 30 14:06:56 2011 +0100 +++ b/src/server/protocol.c Fri Dec 30 15:19:16 2011 +0100 @@ -33,7 +33,7 @@ #include "session.h" #include "io.h" - +#include "strbuf.h" void protocol_status(Session *sn, Request *rq, int n, const char *m) { @@ -226,20 +226,20 @@ } -void add_http_status_line(iovec_buf_t *out, Request *rq) { - iovec_buf_write(out, "HTTP/1.1 ", 9); +void add_http_status_line(sbuf_t *out, pool_handle_t *pool, Request *rq) { + sbuf_write(out, "HTTP/1.1 ", 9); - char *status_code_str = pool_malloc(out->pool, 8); + char *status_code_str = pool_malloc(pool, 8); int sc_len = snprintf(status_code_str, 8, "%d ", rq->status_num); - iovec_buf_write(out, status_code_str, sc_len); + sbuf_write(out, status_code_str, sc_len); char *scmsg = pblock_findkeyval(pb_key_status, rq->srvhdrs); - iovec_buf_write(out, scmsg, strlen(scmsg)); + sbuf_write(out, scmsg, strlen(scmsg)); - iovec_buf_write(out, "\r\n", 2); + sbuf_write(out, "\r\n", 2); } -void add_http_response_header(iovec_buf_t *out, Request *rq) { +void add_http_response_header(sbuf_t *out, Request *rq) { pblock *h = rq->srvhdrs; pb_entry *p; @@ -263,10 +263,10 @@ name[0] -= 32; } - iovec_buf_write(out, name, strlen(name)); - iovec_buf_write(out, ": ", 2); - iovec_buf_write(out, value, strlen(value)); - iovec_buf_write(out, "\r\n", 2); + sbuf_write(out, name, strlen(name)); + sbuf_write(out, ": ", 2); + sbuf_write(out, value, strlen(value)); + sbuf_write(out, "\r\n", 2); p = p->next; } @@ -282,22 +282,23 @@ fcntl(fd, F_SETFL, flags ^ O_NONBLOCK); /* iovec output buffer */ - iovec_buf_t *out = iovec_buf_create(sn->pool); + sbuf_t *out = sbuf_new(512); /* add the http status line to the output buffer */ - add_http_status_line(out, rq); + add_http_status_line(out, sn->pool, rq); /* add server header */ - iovec_buf_write(out, "Server: WS uap-dev\r\n", 20); + sbuf_write(out, "Server: WS uap-dev\r\n", 20); /* add header from rq->srvhdrs */ add_http_response_header(out, rq); /* response header end */ - iovec_buf_write(out, "\r\n", 2); + sbuf_write(out, "\r\n", 2); /* flush buffer to the socket */ - iovec_buf_flush(out, fd); + write(fd, out->ptr, out->length); + sbuf_free(out); rq->senthdrs = 1; } diff -r e3ae779232a9 -r 24d804a2799f src/server/protocol.h --- a/src/server/protocol.h Fri Dec 30 14:06:56 2011 +0100 +++ b/src/server/protocol.h Fri Dec 30 15:19:16 2011 +0100 @@ -32,6 +32,7 @@ #include "nsapi.h" #include "io.h" #include +#include "strbuf.h" #ifdef __cplusplus extern "C" { @@ -40,13 +41,15 @@ void protocol_status(Session *sn, Request *rq, int n, const char *m); const char* protocol_status_message(int code); -void add_http_status_line(iovec_buf_t *out, Request *rq); -void add_http_response_header(iovec_buf_t *out, Request *rq); +void add_http_status_line(sbuf_t *out, pool_handle_t *pool, Request *rq); +void add_http_response_header(sbuf_t *out, Request *rq); int http_start_response(Session *sn, Request *rq); int request_header(char *name, char **value, Session *sn, Request *rq); +#define sbuf_write(out, buf, len) sbuf_append(out, sstrn(buf, len)) + #ifdef __cplusplus } #endif diff -r e3ae779232a9 -r 24d804a2799f src/server/service.c --- a/src/server/service.c Fri Dec 30 14:06:56 2011 +0100 +++ b/src/server/service.c Fri Dec 30 15:19:16 2011 +0100 @@ -139,7 +139,6 @@ int service_hello(pblock *pb, Session *sn, Request *rq) { pblock_nninsert("content-length", 13, rq->srvhdrs); - pblock_nvinsert("content-type", "text/plain", rq->srvhdrs); protocol_status(sn, rq, 200, NULL); http_start_response(sn, rq); net_write(sn->csd, "Hello World!\n", 13); diff -r e3ae779232a9 -r 24d804a2799f src/server/strbuf.c --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/server/strbuf.c Fri Dec 30 15:19:16 2011 +0100 @@ -0,0 +1,71 @@ +/* + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. + * + * Copyright 2011 Olaf Wintermann. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + */ + +#include +#include +#include + +#include "strbuf.h" +#include "sstring.h" + +sbuf_t* sbuf_new(size_t size) { + sbuf_t *buf = malloc(sizeof(sbuf_t)); + + buf->ptr = malloc(size); + buf->ptr[0] = 0; + buf->size = size; + buf->length = 0; + + return buf; +} + +void sbuf_puts(sbuf_t *buf, char *str) { + sbuf_append(buf, sstr(str)); +} + +void sbuf_put(sbuf_t *buf, char chr) { + sbuf_append(buf, sstrn(&chr, 1)); +} + +void sbuf_append(sbuf_t *buf, sstr_t str) { + if (buf->length + str.length >= buf->size) { + buf->size *= 2; + buf->ptr = realloc(buf->ptr, buf->size); + sbuf_append(buf, str); + return; + } + + memcpy(&buf->ptr[buf->length], str.ptr, str.length); + buf->length += str.length; + buf->ptr[buf->length] = 0; +} + +void sbuf_free(sbuf_t *buf) { + free(buf->ptr); + free(buf); +} diff -r e3ae779232a9 -r 24d804a2799f src/server/strbuf.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/server/strbuf.h Fri Dec 30 15:19:16 2011 +0100 @@ -0,0 +1,59 @@ +/* + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. + * + * Copyright 2011 Olaf Wintermann. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + */ + +#ifndef STRBUF_H +#define STRBUF_H + +#include "sstring.h" + +#ifdef __cplusplus +extern "C" { +#endif + +/* string buffer */ +typedef struct _strbuf { + char *ptr; + size_t length; /* length of string */ + size_t size; /* allocated size */ +} sbuf_t; + +sbuf_t* sbuf_new(size_t size); + +void sbuf_puts(sbuf_t *buf, char *str); + +void sbuf_put(sbuf_t *buf, char chr); + +void sbuf_append(sbuf_t *buf, sstr_t str); + +void sbuf_free(sbuf_t *buf); + +#ifdef __cplusplus +} +#endif + +#endif /* STRBUF_H */