From db106357956d84283dc85ea3e47d512da5113db3 Mon Sep 17 00:00:00 2001 From: Wolfgang Draxinger Date: Thu, 14 Mar 2013 12:11:06 +0100 Subject: Thu Mar 14 12:11:06 CET 2013 --- test/bsd_socket.c | 190 ------------------------------------------- test/bsdsocket.c | 190 +++++++++++++++++++++++++++++++++++++++++++ test/bufbsdsocket.c | 229 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 419 insertions(+), 190 deletions(-) delete mode 100644 test/bsd_socket.c create mode 100644 test/bsdsocket.c create mode 100644 test/bufbsdsocket.c diff --git a/test/bsd_socket.c b/test/bsd_socket.c deleted file mode 100644 index d4bd5a2..0000000 --- a/test/bsd_socket.c +++ /dev/null @@ -1,190 +0,0 @@ -#define _BSD_SOURCE - -#include -#include -#include -#include -#include -#include - -#include - -#include -#include -#include - -#include "../picohttp.h" - -int bsdsock_read(size_t count, char *buf, void *data) -{ - int fd = *((int*)data); - - ssize_t rb = 0; - ssize_t r = 0; - do { - r = read(fd, buf+rb, count-rb); - if( 0 < r ) { - rb += r; - continue; - } - if( !r ) { - break; - } - - if( EAGAIN == errno || - EWOULDBLOCK == errno ) { - usleep(100); - continue; - } - return -3 + errno; - } while( rb < count ); - return rb; -} - -int bsdsock_write(size_t count, char const *buf, void *data) -{ - int fd = *((int*)data); - - ssize_t wb = 0; - ssize_t w = 0; - do { - w = write(fd, buf+wb, count-wb); - if( 0 < w ) { - wb += w; - continue; - } - if( !w ) { - break; - } - - if( EAGAIN == errno || - EWOULDBLOCK == errno ) { - usleep(100); - continue; - } - return -3 + errno; - } while( wb < count ); - return wb; -} - -int16_t bsdsock_getch(void *data) -{ - char ch; - if( 1 != bsdsock_read(1, &ch, data) ) - return -1; - return ch; -} - -int bsdsock_putch(char ch, void *data) -{ - return bsdsock_write(1, &ch, data); -} - -int sockfd = -1; - -void bye(void) -{ - fputs("exiting\n", stderr); - int const one = 1; - /* allows for immediate reuse of address:port - * after program termination */ - setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, &one, sizeof(one)); - shutdown(sockfd, SHUT_RDWR); - close(sockfd); -} - -void rhRoot(struct picohttpRequest *req) -{ - fprintf(stderr, "handling request /%s\n", req->urltail); - - char http_header[] = "HTTP/x.x 200 OK\r\nServer: picoweb\r\nContent-Type: text/html\r\n\r\n"; - http_header[5] = '0'+req->httpversion.major; - http_header[7] = '0'+req->httpversion.minor; - picohttpIoWrite(req->ioops, sizeof(http_header)-1, http_header); - char http_test[] = "handling request /\n/test\n"; - - picohttpIoWrite(req->ioops, sizeof(http_test)-1, http_test); -} - -void rhTest(struct picohttpRequest *req) -{ - fprintf(stderr, "handling request /test%s\n", req->urltail); - char http_header[] = "HTTP/x.x 200 OK\r\nServer: picoweb\r\nContent-Type: text/text\r\n\r\n"; - http_header[5] = '0'+req->httpversion.major; - http_header[7] = '0'+req->httpversion.minor; - picohttpIoWrite(req->ioops, sizeof(http_header)-1, http_header); - char http_test[] = "handling request /test"; - picohttpIoWrite(req->ioops, sizeof(http_test)-1, http_test); - if(req->urltail) { - picohttpIoWrite(req->ioops, strlen(req->urltail), req->urltail); - } -} - -int main(int argc, char *argv[]) -{ - sockfd = socket(AF_INET, SOCK_STREAM, 0); - if( -1 == sockfd ) { - perror("socket"); - return -1; - } -#if 0 - if( atexit(bye) ) { - return -1; - } -#endif - - struct sockaddr_in addr = { - .sin_family = AF_INET, - .sin_port = htons(8000), - .sin_addr = 0 - }; - - int const one = 1; - setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, &one, sizeof(one)); - if( -1 == bind(sockfd, (struct sockaddr*)&addr, sizeof(addr)) ) { - perror("bind"); - return -1; - } - - if( -1 == listen(sockfd, 2) ) { - perror("listen"); - return -1; - } - - for(;;) { - socklen_t addrlen = 0; - int confd = accept(sockfd, (struct sockaddr*)&addr, &addrlen); - if( -1 == confd ) { - if( EAGAIN == errno || - EWOULDBLOCK == errno ) { - usleep(1000); - continue; - } else { - perror("accept"); - return -1; - } - } - - struct picohttpIoOps ioops = { - .read = bsdsock_read, - .write = bsdsock_write, - .getch = bsdsock_getch, - .putch = bsdsock_putch, - .data = &confd - }; - - struct picohttpURLRoute routes[] = { - { "/test", 0, rhTest, 16, PICOHTTP_METHOD_GET }, - { "/|", 0, rhRoot, 0, PICOHTTP_METHOD_GET }, - { NULL, 0, 0, 0, 0 } - }; - - picohttpProcessRequest(&ioops, routes); - - shutdown(confd, SHUT_RDWR); - close(confd); - } - - return 0; -} - diff --git a/test/bsdsocket.c b/test/bsdsocket.c new file mode 100644 index 0000000..d4bd5a2 --- /dev/null +++ b/test/bsdsocket.c @@ -0,0 +1,190 @@ +#define _BSD_SOURCE + +#include +#include +#include +#include +#include +#include + +#include + +#include +#include +#include + +#include "../picohttp.h" + +int bsdsock_read(size_t count, char *buf, void *data) +{ + int fd = *((int*)data); + + ssize_t rb = 0; + ssize_t r = 0; + do { + r = read(fd, buf+rb, count-rb); + if( 0 < r ) { + rb += r; + continue; + } + if( !r ) { + break; + } + + if( EAGAIN == errno || + EWOULDBLOCK == errno ) { + usleep(100); + continue; + } + return -3 + errno; + } while( rb < count ); + return rb; +} + +int bsdsock_write(size_t count, char const *buf, void *data) +{ + int fd = *((int*)data); + + ssize_t wb = 0; + ssize_t w = 0; + do { + w = write(fd, buf+wb, count-wb); + if( 0 < w ) { + wb += w; + continue; + } + if( !w ) { + break; + } + + if( EAGAIN == errno || + EWOULDBLOCK == errno ) { + usleep(100); + continue; + } + return -3 + errno; + } while( wb < count ); + return wb; +} + +int16_t bsdsock_getch(void *data) +{ + char ch; + if( 1 != bsdsock_read(1, &ch, data) ) + return -1; + return ch; +} + +int bsdsock_putch(char ch, void *data) +{ + return bsdsock_write(1, &ch, data); +} + +int sockfd = -1; + +void bye(void) +{ + fputs("exiting\n", stderr); + int const one = 1; + /* allows for immediate reuse of address:port + * after program termination */ + setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, &one, sizeof(one)); + shutdown(sockfd, SHUT_RDWR); + close(sockfd); +} + +void rhRoot(struct picohttpRequest *req) +{ + fprintf(stderr, "handling request /%s\n", req->urltail); + + char http_header[] = "HTTP/x.x 200 OK\r\nServer: picoweb\r\nContent-Type: text/html\r\n\r\n"; + http_header[5] = '0'+req->httpversion.major; + http_header[7] = '0'+req->httpversion.minor; + picohttpIoWrite(req->ioops, sizeof(http_header)-1, http_header); + char http_test[] = "handling request /\n/test\n"; + + picohttpIoWrite(req->ioops, sizeof(http_test)-1, http_test); +} + +void rhTest(struct picohttpRequest *req) +{ + fprintf(stderr, "handling request /test%s\n", req->urltail); + char http_header[] = "HTTP/x.x 200 OK\r\nServer: picoweb\r\nContent-Type: text/text\r\n\r\n"; + http_header[5] = '0'+req->httpversion.major; + http_header[7] = '0'+req->httpversion.minor; + picohttpIoWrite(req->ioops, sizeof(http_header)-1, http_header); + char http_test[] = "handling request /test"; + picohttpIoWrite(req->ioops, sizeof(http_test)-1, http_test); + if(req->urltail) { + picohttpIoWrite(req->ioops, strlen(req->urltail), req->urltail); + } +} + +int main(int argc, char *argv[]) +{ + sockfd = socket(AF_INET, SOCK_STREAM, 0); + if( -1 == sockfd ) { + perror("socket"); + return -1; + } +#if 0 + if( atexit(bye) ) { + return -1; + } +#endif + + struct sockaddr_in addr = { + .sin_family = AF_INET, + .sin_port = htons(8000), + .sin_addr = 0 + }; + + int const one = 1; + setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, &one, sizeof(one)); + if( -1 == bind(sockfd, (struct sockaddr*)&addr, sizeof(addr)) ) { + perror("bind"); + return -1; + } + + if( -1 == listen(sockfd, 2) ) { + perror("listen"); + return -1; + } + + for(;;) { + socklen_t addrlen = 0; + int confd = accept(sockfd, (struct sockaddr*)&addr, &addrlen); + if( -1 == confd ) { + if( EAGAIN == errno || + EWOULDBLOCK == errno ) { + usleep(1000); + continue; + } else { + perror("accept"); + return -1; + } + } + + struct picohttpIoOps ioops = { + .read = bsdsock_read, + .write = bsdsock_write, + .getch = bsdsock_getch, + .putch = bsdsock_putch, + .data = &confd + }; + + struct picohttpURLRoute routes[] = { + { "/test", 0, rhTest, 16, PICOHTTP_METHOD_GET }, + { "/|", 0, rhRoot, 0, PICOHTTP_METHOD_GET }, + { NULL, 0, 0, 0, 0 } + }; + + picohttpProcessRequest(&ioops, routes); + + shutdown(confd, SHUT_RDWR); + close(confd); + } + + return 0; +} + diff --git a/test/bufbsdsocket.c b/test/bufbsdsocket.c new file mode 100644 index 0000000..27ce6ef --- /dev/null +++ b/test/bufbsdsocket.c @@ -0,0 +1,229 @@ +#define _BSD_SOURCE + +#include +#include +#include +#include +#include +#include + +#include + +#include +#include +#include + +#include "../picohttp.h" + +#define SENDBUF_LEN 256 + +struct bufbsdsockData { + char * recvbuf; + size_t recvbuf_len; + size_t recvbuf_pos; + char sendbuf[SENDBUF_LEN]; + size_t sendbuf_pos; + int fd; +}; + +int bufbsdsock_read(size_t count, char *buf, void *data_) +{ + struct bufbsdsockData *data = data_; + + ssize_t rb = 0; + ssize_t r = 0; + do { + size_t len = 0; + + if( !data->recvbuf || + data->recvbuf_pos >= data->recvbuf_len ) { + if( data->recvbuf ) + free( data->recvbuf ); + data->recvbuf_len = 0; + data->recvbuf_pos = 0; + + int avail = 0; + do { + struct pollfd pfd = { + .fd = data->fd, + .events = POLLIN | POLLPRI, + .revents = 0 + }; + + int const pret = poll(&pfd, 1, -1); + if( 0 >= pret ) { + return -1; + } + + assert(pfd.revents & (POLLIN | POLLPRI)); + + if( -1 == ioctl(fd, FIONREAD, &avail) ) { + perror("ioctl(FIONREAD)"); + return -1; + } + } while( !avail ); + + data->recvbuf = malloc( avail); + + int r; + while( 0 > (r = read(data->fd, data->recvbuf, avail)) ) + if( EINTR == errno ) + continue; + + if( EAGAIN == errno || + EWOULDBLOCK == errno ) { + usleep(200); + continue; + } + + return -1; + } + data->recvbuf_len += r; + } + + len = data->recvbuf_len - data->recvbuf_pos; + if( len > count ) + len = count; + + rb += len; + } while( rb < count ); + return rb; +} + +int bufbsdsock_write(size_t count, char const *buf, void *data) +{ + int fd = *((int*)data); + + ssize_t wb = 0; + ssize_t w = 0; + do { + } while( wb < count ); + return wb; +} + +int16_t bufbsdsock_getch(void *data) +{ + char ch; + if( 1 != bufbsdsock_read(1, &ch, data) ) + return -1; + return ch; +} + +int bufbsdsock_putch(char ch, void *data) +{ + return bufbsdsock_write(1, &ch, data); +} + +int bufbsdsock_flush(void *data) +{ + return 0; +} + +int sockfd = -1; + +void bye(void) +{ + fputs("exiting\n", stderr); + int const one = 1; + /* allows for immediate reuse of address:port + * after program termination */ + setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, &one, sizeof(one)); + shutdown(sockfd, SHUT_RDWR); + close(sockfd); +} + +void rhRoot(struct picohttpRequest *req) +{ + fprintf(stderr, "handling request /%s\n", req->urltail); + + char http_header[] = "HTTP/x.x 200 OK\r\nServer: picoweb\r\nContent-Type: text/html\r\n\r\n"; + http_header[5] = '0'+req->httpversion.major; + http_header[7] = '0'+req->httpversion.minor; + picohttpIoWrite(req->ioops, sizeof(http_header)-1, http_header); + char http_test[] = "handling request /\n/test\n"; + + picohttpIoWrite(req->ioops, sizeof(http_test)-1, http_test); +} + +void rhTest(struct picohttpRequest *req) +{ + fprintf(stderr, "handling request /test%s\n", req->urltail); + char http_header[] = "HTTP/x.x 200 OK\r\nServer: picoweb\r\nContent-Type: text/text\r\n\r\n"; + http_header[5] = '0'+req->httpversion.major; + http_header[7] = '0'+req->httpversion.minor; + picohttpIoWrite(req->ioops, sizeof(http_header)-1, http_header); + char http_test[] = "handling request /test"; + picohttpIoWrite(req->ioops, sizeof(http_test)-1, http_test); + if(req->urltail) { + picohttpIoWrite(req->ioops, strlen(req->urltail), req->urltail); + } +} + +int main(int argc, char *argv[]) +{ + sockfd = socket(AF_INET, SOCK_STREAM, 0); + if( -1 == sockfd ) { + perror("socket"); + return -1; + } +#if 0 + if( atexit(bye) ) { + return -1; + } +#endif + + struct sockaddr_in addr = { + .sin_family = AF_INET, + .sin_port = htons(8000), + .sin_addr = 0 + }; + + int const one = 1; + setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, &one, sizeof(one)); + if( -1 == bind(sockfd, (struct sockaddr*)&addr, sizeof(addr)) ) { + perror("bind"); + return -1; + } + + if( -1 == listen(sockfd, 2) ) { + perror("listen"); + return -1; + } + + for(;;) { + socklen_t addrlen = 0; + int confd = accept(sockfd, (struct sockaddr*)&addr, &addrlen); + if( -1 == confd ) { + if( EAGAIN == errno || + EWOULDBLOCK == errno ) { + usleep(1000); + continue; + } else { + perror("accept"); + return -1; + } + } + + struct picohttpIoOps ioops = { + .read = bsdsock_read, + .write = bsdsock_write, + .getch = bsdsock_getch, + .putch = bsdsock_putch, + .data = &confd + }; + + struct picohttpURLRoute routes[] = { + { "/test", 0, rhTest, 16, PICOHTTP_METHOD_GET }, + { "/|", 0, rhRoot, 0, PICOHTTP_METHOD_GET }, + { NULL, 0, 0, 0, 0 } + }; + + picohttpProcessRequest(&ioops, routes); + + shutdown(confd, SHUT_RDWR); + close(confd); + } + + return 0; +} + -- cgit v1.2.3 From 813d72a2d1a4305811b39607828a9f49daadb2ab Mon Sep 17 00:00:00 2001 From: Wolfgang Draxinger Date: Tue, 18 Jun 2013 00:53:25 +0200 Subject: Added API declarations for Chunked Tranfer transport and Multipart Encoding support. --- picohttp.h | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/picohttp.h b/picohttp.h index 041f124..d94172b 100644 --- a/picohttp.h +++ b/picohttp.h @@ -127,4 +127,15 @@ int picohttpResponseWrite ( size_t len, char const *buf ); +uint16_t picohttpGetch( + struct picohttpRequest * const req, + struct picohttpChunkTransfer * const ct); + +int picohttpMultipartNext( + struct picohttpRequest * const req, + struct picohttpMultiPart * const mp); + +uint16_t picohttpMultipartGetch( + struct picohttpMultiPart * const mp); + #endif/*PICOHTTP_H_HEADERGUARD*/ -- cgit v1.2.3 From fc9f3c0bde26bc882bdfe1ec0435ade8b60172f4 Mon Sep 17 00:00:00 2001 From: Wolfgang Draxinger Date: Tue, 18 Jun 2013 00:55:27 +0200 Subject: Epoch for datetime struct changed to 1970 --- picohttp.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/picohttp.h b/picohttp.h index 95ac0e7..803587d 100644 --- a/picohttp.h +++ b/picohttp.h @@ -79,7 +79,7 @@ struct picohttpURLRoute { int16_t allowed_methods; }; -#define PICOHTTP_EPOCH_YEAR 1980 +#define PICOHTTP_EPOCH_YEAR 1970 struct picohttpDateTime { unsigned int Y:7; /* EPOCH + 127 years */ -- cgit v1.2.3