Commit 5b3fed549faa460d333f375948db7b5635a74c58

Authored by Georg Hopp
1 parent 58eb487e

refs #19: Added a stream class. This is a delegate that uses the correct read an…

…d write methods for the given stream type. This is implemented as prerequesite for the SSL implemented
@@ -20,6 +20,8 @@ AM_PROG_CC_C_O @@ -20,6 +20,8 @@ AM_PROG_CC_C_O
20 # # Checks for libraries. 20 # # Checks for libraries.
21 # AC_CHECK_LIB([json], [json_object_new_object], [], 21 # AC_CHECK_LIB([json], [json_object_new_object], [],
22 # [AC_MSG_ERROR([json-c library not found], [1])]) 22 # [AC_MSG_ERROR([json-c library not found], [1])])
  23 +AC_CHECK_LIB([ssl], [SSL_library_init], [],
  24 + [AC_MSG_ERROR([openssl not found], [1])])
23 25
24 # Checks for header files. 26 # Checks for header files.
25 AC_CHECK_HEADERS([stdarg.h string.h stdlib.h stdio.h unistd.h syslog.h sys/types.h json/json.h]) 27 AC_CHECK_HEADERS([stdarg.h string.h stdlib.h stdio.h unistd.h syslog.h sys/types.h json/json.h])
@@ -35,22 +35,18 @@ @@ -35,22 +35,18 @@
35 #include <sys/types.h> 35 #include <sys/types.h>
36 36
37 #include "class.h" 37 #include "class.h"
  38 +#include "stream.h"
38 39
39 -#define ECBUFOVFL 100 40 +#include "commons.h"
40 41
41 -#ifndef TRUE  
42 -#define TRUE ((void *)1)  
43 -#endif 42 +#define ECBUFOVFL 100
44 43
45 -#ifndef FALSE  
46 -#define FALSE ((void *)0)  
47 -#endif  
48 44
49 CLASS(Cbuf) { 45 CLASS(Cbuf) {
50 char * shm_name; // shared memory identifier 46 char * shm_name; // shared memory identifier
51 47
52 char * data; 48 char * data;
53 - void * lock; 49 + Bool lock;
54 50
55 size_t bsize; 51 size_t bsize;
56 size_t bused; 52 size_t bused;
@@ -59,8 +55,8 @@ CLASS(Cbuf) { @@ -59,8 +55,8 @@ CLASS(Cbuf) {
59 size_t read; 55 size_t read;
60 }; 56 };
61 57
62 -ssize_t cbufRead(Cbuf, int fd);  
63 -ssize_t cbufWrite(Cbuf, int fd); 58 +ssize_t cbufRead(Cbuf, Stream);
  59 +ssize_t cbufWrite(Cbuf, Stream);
64 60
65 char * cbufGetLine(Cbuf, char **); 61 char * cbufGetLine(Cbuf, char **);
66 char * cbufGetData(Cbuf, size_t); 62 char * cbufGetData(Cbuf, size_t);
@@ -76,7 +72,7 @@ void cbufIncWrite(Cbuf this, size_t n); @@ -76,7 +72,7 @@ void cbufIncWrite(Cbuf this, size_t n);
76 size_t cbufGetFree(Cbuf this); 72 size_t cbufGetFree(Cbuf this);
77 char cbufIsEmpty(Cbuf this); 73 char cbufIsEmpty(Cbuf this);
78 void cbufSkipNonAlpha(Cbuf this); 74 void cbufSkipNonAlpha(Cbuf this);
79 -void * cbufIsLocked(Cbuf this); 75 +Bool cbufIsLocked(Cbuf this);
80 void cbufLock(Cbuf this); 76 void cbufLock(Cbuf this);
81 void cbufRelease(Cbuf this); 77 void cbufRelease(Cbuf this);
82 78
  1 +#ifndef __COMMONS_H__
  2 +#define __COMMONS_H__
  3 +
  4 +#define Bool char
  5 +#define TRUE 1
  6 +#define FALSE 0
  7 +
  8 +#endif // __COMMONS_H__
  9 +
  10 +// vim: set ts=4 sw=4:
@@ -26,6 +26,7 @@ @@ -26,6 +26,7 @@
26 26
27 #include "class.h" 27 #include "class.h"
28 #include "http/header.h" 28 #include "http/header.h"
  29 +#include "stream.h"
29 30
30 typedef enum e_HttpMessageType { 31 typedef enum e_HttpMessageType {
31 HTTP_MESSAGE_BUFFERED=0, 32 HTTP_MESSAGE_BUFFERED=0,
@@ -39,8 +40,8 @@ CLASS(HttpMessage) { @@ -39,8 +40,8 @@ CLASS(HttpMessage) {
39 HttpHeader header; 40 HttpHeader header;
40 41
41 HttpMessageType type; 42 HttpMessageType type;
42 - int handle;  
43 - char * body; 43 + Stream handle;
  44 + char * body;
44 int nbody; 45 int nbody;
45 int dbody; 46 int dbody;
46 }; 47 };
@@ -28,14 +28,9 @@ @@ -28,14 +28,9 @@
28 #include "http/message.h" 28 #include "http/message.h"
29 #include "http/message/queue.h" 29 #include "http/message/queue.h"
30 #include "cbuf.h" 30 #include "cbuf.h"
  31 +#include "stream.h"
31 32
32 -#ifndef TRUE  
33 -#define TRUE ((void *)1)  
34 -#endif  
35 -  
36 -#ifndef FALSE  
37 -#define FALSE ((void *)0)  
38 -#endif 33 +#include "commons.h"
39 34
40 #define PARSER_MAX_BUF 131072 35 #define PARSER_MAX_BUF 131072
41 36
@@ -51,7 +46,7 @@ typedef enum e_HttpMessageState { @@ -51,7 +46,7 @@ typedef enum e_HttpMessageState {
51 46
52 CLASS(HttpParser) { 47 CLASS(HttpParser) {
53 Cbuf buffer; 48 Cbuf buffer;
54 - void * ourLock; 49 + Bool ourLock;
55 50
56 char * incomplete; 51 char * incomplete;
57 size_t isize; 52 size_t isize;
@@ -62,7 +57,7 @@ CLASS(HttpParser) { @@ -62,7 +57,7 @@ CLASS(HttpParser) {
62 HttpMessageState state; 57 HttpMessageState state;
63 }; 58 };
64 59
65 -ssize_t httpParserParse(void *, int); 60 +ssize_t httpParserParse(void *, Stream);
66 void httpParserHeader(HttpParser, const char *, const char *); 61 void httpParserHeader(HttpParser, const char *, const char *);
67 void httpParserNewMessage(HttpParser, const char *, const char * lend); 62 void httpParserNewMessage(HttpParser, const char *, const char * lend);
68 size_t httpParserBody(HttpParser, const char *, size_t); 63 size_t httpParserBody(HttpParser, const char *, size_t);
@@ -33,14 +33,7 @@ @@ -33,14 +33,7 @@
33 #include "cbuf.h" 33 #include "cbuf.h"
34 #include "session.h" 34 #include "session.h"
35 35
36 -  
37 -#ifndef TRUE  
38 -#define TRUE ((void *)1)  
39 -#endif  
40 -  
41 -#ifndef FALSE  
42 -#define FALSE ((void *)0)  
43 -#endif 36 +#include "commons.h"
44 37
45 struct randval { 38 struct randval {
46 time_t timestamp; 39 time_t timestamp;
@@ -30,14 +30,10 @@ @@ -30,14 +30,10 @@
30 #include "http/message.h" 30 #include "http/message.h"
31 #include "http/message/queue.h" 31 #include "http/message/queue.h"
32 #include "cbuf.h" 32 #include "cbuf.h"
  33 +#include "stream.h"
33 34
34 -#ifndef TRUE  
35 -#define TRUE ((void *)1)  
36 -#endif 35 +#include "commons.h"
37 36
38 -#ifndef FALSE  
39 -#define FALSE ((void *)0)  
40 -#endif  
41 37
42 #define WRITER_MAX_BUF 131072 38 #define WRITER_MAX_BUF 131072
43 39
@@ -50,7 +46,7 @@ typedef enum e_HttpWriterState { @@ -50,7 +46,7 @@ typedef enum e_HttpWriterState {
50 46
51 CLASS(HttpWriter) { 47 CLASS(HttpWriter) {
52 Cbuf buffer; 48 Cbuf buffer;
53 - void * ourLock; 49 + Bool ourLock;
54 50
55 HttpMessageQueue queue; 51 HttpMessageQueue queue;
56 HttpMessage current; 52 HttpMessage current;
@@ -62,7 +58,7 @@ CLASS(HttpWriter) { @@ -62,7 +58,7 @@ CLASS(HttpWriter) {
62 HttpWriterState state; 58 HttpWriterState state;
63 }; 59 };
64 60
65 -ssize_t httpWriterWrite(void *, int); 61 +ssize_t httpWriterWrite(void *, Stream);
66 62
67 #endif // __HTTP_WRITER_H__ 63 #endif // __HTTP_WRITER_H__
68 64
@@ -27,7 +27,9 @@ @@ -27,7 +27,9 @@
27 27
28 #include <sys/types.h> 28 #include <sys/types.h>
29 29
30 -typedef ssize_t (* fptr_streamReaderRead)(void *, int fd); 30 +#include "stream.h"
  31 +
  32 +typedef ssize_t (* fptr_streamReaderRead)(void *, Stream);
31 33
32 extern const struct interface i_StreamReader; 34 extern const struct interface i_StreamReader;
33 35
@@ -36,7 +38,7 @@ struct i_StreamReader { @@ -36,7 +38,7 @@ struct i_StreamReader {
36 fptr_streamReaderRead read; 38 fptr_streamReaderRead read;
37 }; 39 };
38 40
39 -extern ssize_t streamReaderRead(void *, int fd); 41 +extern ssize_t streamReaderRead(void *, Stream);
40 42
41 #endif // __STREAM_READER_H__ 43 #endif // __STREAM_READER_H__
42 44
@@ -27,7 +27,9 @@ @@ -27,7 +27,9 @@
27 27
28 #include <sys/types.h> 28 #include <sys/types.h>
29 29
30 -typedef ssize_t (* fptr_streamWriterWrite)(void *, int fd); 30 +#include "stream.h"
  31 +
  32 +typedef ssize_t (* fptr_streamWriterWrite)(void *, Stream);
31 33
32 extern const struct interface i_StreamWriter; 34 extern const struct interface i_StreamWriter;
33 35
@@ -36,7 +38,7 @@ struct i_StreamWriter { @@ -36,7 +38,7 @@ struct i_StreamWriter {
36 fptr_streamWriterWrite write; 38 fptr_streamWriterWrite write;
37 }; 39 };
38 40
39 -extern ssize_t streamWriterWrite(void *, int fd); 41 +extern ssize_t streamWriterWrite(void *, Stream);
40 42
41 #endif // __STREAM_WRITER_H__ 43 #endif // __STREAM_WRITER_H__
42 44
@@ -32,9 +32,11 @@ @@ -32,9 +32,11 @@
32 #include "class.h" 32 #include "class.h"
33 #include "socket.h" 33 #include "socket.h"
34 #include "logger.h" 34 #include "logger.h"
  35 +#include "stream.h"
35 36
36 struct conns { 37 struct conns {
37 Sock sock; 38 Sock sock;
  39 + Stream stream;
38 void * worker; 40 void * worker;
39 }; 41 };
40 42
  1 +#ifndef __STREAM_H__
  2 +#define __STREAM_H__
  3 +
  4 +#include <sys/types.h>
  5 +#include <openssl/ssl.h>
  6 +
  7 +#include "class.h"
  8 +
  9 +typedef enum e_StreamHandleType {
  10 + STREAM_FD = 0,
  11 + STREAM_SSL
  12 +} StreamHandleType;
  13 +
  14 +CLASS(Stream) {
  15 + StreamHandleType type;
  16 + union {
  17 + int fd;
  18 + SSL * ssl;
  19 + } handle;
  20 +};
  21 +
  22 +ssize_t streamRead(Stream, void *, size_t);
  23 +ssize_t streamWrite(Stream, void *, size_t);
  24 +
  25 +#endif // __STREAM_H__
  26 +
  27 +// vim: set ts=4 sw=4:
@@ -5,6 +5,7 @@ IFACE = interface/class.c interface/stream_reader.c interface/logger.c \ @@ -5,6 +5,7 @@ IFACE = interface/class.c interface/stream_reader.c interface/logger.c \
5 interface/stream_writer.c interface/http_intro.c \ 5 interface/stream_writer.c interface/http_intro.c \
6 interface/subject.c interface/observer.c interface.c 6 interface/subject.c interface/observer.c interface.c
7 SOCKET = socket.c socket/accept.c socket/connect.c socket/listen.c 7 SOCKET = socket.c socket/accept.c socket/connect.c socket/listen.c
  8 +STREAM = stream.c stream/read.c stream/write.c
8 SERVER = server.c server/run.c server/close_conn.c server/poll.c \ 9 SERVER = server.c server/run.c server/close_conn.c server/poll.c \
9 server/handle_accept.c server/read.c server/write.c 10 server/handle_accept.c server/read.c server/write.c
10 LOGGER = logger.c logger/stderr.c logger/syslog.c 11 LOGGER = logger.c logger/stderr.c logger/syslog.c
@@ -60,6 +61,6 @@ bin_PROGRAMS = webgameserver @@ -60,6 +61,6 @@ bin_PROGRAMS = webgameserver
60 webgameserver_SOURCES = webgameserver.c \ 61 webgameserver_SOURCES = webgameserver.c \
61 $(IFACE) $(SOCKET) $(SERVER) $(LOGGER) $(MSG) $(REQ) \ 62 $(IFACE) $(SOCKET) $(SERVER) $(LOGGER) $(MSG) $(REQ) \
62 $(WRITER) $(RESP) $(HEADER) $(PARSER) $(WORKER) $(CB) \ 63 $(WRITER) $(RESP) $(HEADER) $(PARSER) $(WORKER) $(CB) \
63 - $(UTILS) $(MSGQ) $(SESSION) 64 + $(UTILS) $(MSGQ) $(SESSION) $(STREAM)
64 webgameserver_CFLAGS = -Wall -I ../include/ 65 webgameserver_CFLAGS = -Wall -I ../include/
65 -webgameserver_LDFLAGS = -lrt 66 +webgameserver_LDFLAGS = -lrt -lssl
@@ -22,7 +22,9 @@ @@ -22,7 +22,9 @@
22 22
23 #include "cbuf.h" 23 #include "cbuf.h"
24 24
25 -void * 25 +#include "commons.h"
  26 +
  27 +Bool
26 cbufIsLocked(Cbuf this) 28 cbufIsLocked(Cbuf this)
27 { 29 {
28 return this->lock; 30 return this->lock;
@@ -25,10 +25,11 @@ @@ -25,10 +25,11 @@
25 #include <errno.h> 25 #include <errno.h>
26 26
27 #include "cbuf.h" 27 #include "cbuf.h"
  28 +#include "stream.h"
28 29
29 30
30 ssize_t 31 ssize_t
31 -cbufRead(Cbuf this, int fd) 32 +cbufRead(Cbuf this, Stream st)
32 { 33 {
33 ssize_t rrsize = 0; 34 ssize_t rrsize = 0;
34 size_t rsize = cbufGetFree(this); 35 size_t rsize = cbufGetFree(this);
@@ -38,7 +39,7 @@ cbufRead(Cbuf this, int fd) @@ -38,7 +39,7 @@ cbufRead(Cbuf this, int fd)
38 return -1; 39 return -1;
39 } 40 }
40 41
41 - rrsize = read(fd, cbufGetWrite(this), rsize); 42 + rrsize = streamRead(st, cbufGetWrite(this), rsize);
42 43
43 switch (rrsize) { 44 switch (rrsize) {
44 case 0: 45 case 0:
@@ -24,17 +24,18 @@ @@ -24,17 +24,18 @@
24 #include <unistd.h> 24 #include <unistd.h>
25 25
26 #include "cbuf.h" 26 #include "cbuf.h"
  27 +#include "stream.h"
27 28
28 29
29 ssize_t 30 ssize_t
30 -cbufWrite(Cbuf this, int fd) 31 +cbufWrite(Cbuf this, Stream st)
31 { 32 {
32 ssize_t wwsize = 0; 33 ssize_t wwsize = 0;
33 size_t wsize = this->bused; 34 size_t wsize = this->bused;
34 35
35 if (0 == wsize) return 0; 36 if (0 == wsize) return 0;
36 37
37 - wwsize = write(fd, cbufGetRead(this), wsize); 38 + wwsize = streamWrite(st, cbufGetRead(this), wsize);
38 39
39 switch (wwsize) { 40 switch (wwsize) {
40 case -1: 41 case -1:
@@ -79,7 +79,10 @@ httpMessageDtor(void * _this) @@ -79,7 +79,10 @@ httpMessageDtor(void * _this)
79 break; 79 break;
80 80
81 case HTTP_MESSAGE_PIPED: 81 case HTTP_MESSAGE_PIPED:
82 - if (2 < this->handle) close(this->handle); 82 + if (2 < (this->handle->handle).fd) {
  83 + close((this->handle->handle).fd);
  84 + }
  85 + delete(this->handle);
83 break; 86 break;
84 87
85 default: 88 default:
@@ -30,13 +30,7 @@ @@ -30,13 +30,7 @@
30 30
31 #include "utils/memory.h" 31 #include "utils/memory.h"
32 32
33 -#ifndef TRUE  
34 -#define TRUE 1  
35 -#endif  
36 -  
37 -#ifndef FALSE  
38 -#define FALSE 0  
39 -#endif 33 +#include "commons.h"
40 34
41 char 35 char
42 httpMessageHasKeepAlive(HttpMessage message) 36 httpMessageHasKeepAlive(HttpMessage message)
@@ -26,9 +26,10 @@ @@ -26,9 +26,10 @@
26 #include "interface/class.h" 26 #include "interface/class.h"
27 #include "interface/http_intro.h" 27 #include "interface/http_intro.h"
28 #include "cbuf.h" 28 #include "cbuf.h"
  29 +#include "stream.h"
29 30
30 ssize_t 31 ssize_t
31 -httpParserParse(void * _this, int fd) 32 +httpParserParse(void * _this, Stream st)
32 { 33 {
33 HttpParser this = _this; 34 HttpParser this = _this;
34 int cont = 1; 35 int cont = 1;
@@ -51,7 +52,7 @@ httpParserParse(void * _this, int fd) @@ -51,7 +52,7 @@ httpParserParse(void * _this, int fd)
51 this->incomplete = NULL; 52 this->incomplete = NULL;
52 } 53 }
53 54
54 - if (0 > (read = cbufRead(this->buffer, fd))) { 55 + if (0 > (read = cbufRead(this->buffer, st))) {
55 return read; 56 return read;
56 } 57 }
57 58
@@ -28,6 +28,7 @@ @@ -28,6 +28,7 @@
28 28
29 #include "class.h" 29 #include "class.h"
30 #include "interface/class.h" 30 #include "interface/class.h"
  31 +#include "stream.h"
31 32
32 #include "http/response.h" 33 #include "http/response.h"
33 #include "http/message.h" 34 #include "http/message.h"
@@ -69,7 +70,7 @@ httpResponseAsset( @@ -69,7 +70,7 @@ httpResponseAsset(
69 message = (HttpMessage)response; 70 message = (HttpMessage)response;
70 71
71 message->type = HTTP_MESSAGE_PIPED; 72 message->type = HTTP_MESSAGE_PIPED;
72 - message->handle = handle; 73 + message->handle = new(Stream, STREAM_FD, handle);
73 message->nbody = st.st_size; 74 message->nbody = st.st_size;
74 75
75 httpHeaderAdd(&(message->header), 76 httpHeaderAdd(&(message->header),
@@ -29,6 +29,7 @@ @@ -29,6 +29,7 @@
29 #include <search.h> 29 #include <search.h>
30 30
31 #include "class.h" 31 #include "class.h"
  32 +#include "stream.h"
32 #include "http/worker.h" 33 #include "http/worker.h"
33 #include "http/parser.h" 34 #include "http/parser.h"
34 #include "http/writer.h" 35 #include "http/writer.h"
@@ -107,8 +108,8 @@ httpWorkerClone(void * _this, void * _base) @@ -107,8 +108,8 @@ httpWorkerClone(void * _this, void * _base)
107 this->sroot = &(base->session); 108 this->sroot = &(base->session);
108 } 109 }
109 110
110 -ssize_t httpWorkerProcess(void *, int);  
111 -ssize_t httpWorkerWrite(void *, int); 111 +ssize_t httpWorkerProcess(void *, Stream);
  112 +ssize_t httpWorkerWrite(void *, Stream);
112 113
113 INIT_IFACE(Class, httpWorkerCtor, httpWorkerDtor, httpWorkerClone); 114 INIT_IFACE(Class, httpWorkerCtor, httpWorkerDtor, httpWorkerClone);
114 INIT_IFACE(StreamReader, httpWorkerProcess); 115 INIT_IFACE(StreamReader, httpWorkerProcess);
@@ -35,6 +35,7 @@ @@ -35,6 +35,7 @@
35 #include "http/message/queue.h" 35 #include "http/message/queue.h"
36 #include "http/parser.h" 36 #include "http/parser.h"
37 #include "session.h" 37 #include "session.h"
  38 +#include "stream.h"
38 39
39 #include "utils/memory.h" 40 #include "utils/memory.h"
40 41
@@ -42,14 +43,14 @@ HttpMessage httpWorkerGetAsset(HttpRequest, const char *, const char *, size_t); @@ -42,14 +43,14 @@ HttpMessage httpWorkerGetAsset(HttpRequest, const char *, const char *, size_t);
42 void httpWorkerAddCommonHeader(HttpMessage, HttpMessage); 43 void httpWorkerAddCommonHeader(HttpMessage, HttpMessage);
43 44
44 ssize_t 45 ssize_t
45 -httpWorkerProcess(HttpWorker this, int fd) 46 +httpWorkerProcess(HttpWorker this, Stream st)
46 { 47 {
47 time_t t; 48 time_t t;
48 struct tm * tmp; 49 struct tm * tmp;
49 char buffer[200]; 50 char buffer[200];
50 ssize_t size; 51 ssize_t size;
51 52
52 - if (0 < (size = httpParserParse(this->parser, fd))) { 53 + if (0 < (size = httpParserParse(this->parser, st))) {
53 int i; 54 int i;
54 HttpMessageQueue reqq = this->parser->queue; 55 HttpMessageQueue reqq = this->parser->queue;
55 HttpMessageQueue respq = this->writer->queue; 56 HttpMessageQueue respq = this->writer->queue;
@@ -92,7 +93,6 @@ httpWorkerProcess(HttpWorker this, int fd) @@ -92,7 +93,6 @@ httpWorkerProcess(HttpWorker this, int fd)
92 if (0 == strcmp("POST", request->method)) { 93 if (0 == strcmp("POST", request->method)) {
93 if (0 == strcmp("/me/", request->uri)) { 94 if (0 == strcmp("/me/", request->uri)) {
94 char * delim = memchr(rmessage->body, '=', rmessage->nbody); 95 char * delim = memchr(rmessage->body, '=', rmessage->nbody);
95 - char * key = rmessage->body;  
96 char * val; 96 char * val;
97 size_t nkey, nval; 97 size_t nkey, nval;
98 char buffer[200]; 98 char buffer[200];
@@ -24,11 +24,12 @@ @@ -24,11 +24,12 @@
24 24
25 #include "http/worker.h" 25 #include "http/worker.h"
26 #include "http/writer.h" 26 #include "http/writer.h"
  27 +#include "stream.h"
27 28
28 ssize_t 29 ssize_t
29 -httpWorkerWrite(HttpWorker this, int fd) 30 +httpWorkerWrite(HttpWorker this, Stream st)
30 { 31 {
31 - return httpWriterWrite(this->writer, fd); 32 + return httpWriterWrite(this->writer, st);
32 } 33 }
33 34
34 // vim: set ts=4 sw=4: 35 // vim: set ts=4 sw=4:
@@ -28,12 +28,13 @@ @@ -28,12 +28,13 @@
28 #include "http/message.h" 28 #include "http/message.h"
29 #include "http/writer.h" 29 #include "http/writer.h"
30 #include "cbuf.h" 30 #include "cbuf.h"
  31 +#include "stream.h"
31 32
32 #define MIN(x,y) ((x) < (y) ? (x) : (y)) 33 #define MIN(x,y) ((x) < (y) ? (x) : (y))
33 #define MAX(x,y) ((x) > (y) ? (x) : (y)) 34 #define MAX(x,y) ((x) > (y) ? (x) : (y))
34 35
35 ssize_t 36 ssize_t
36 -httpWriterWrite(void * _this, int fd) 37 +httpWriterWrite(void * _this, Stream st)
37 { 38 {
38 HttpWriter this = _this; 39 HttpWriter this = _this;
39 HttpMessageQueue respq = this->queue; 40 HttpMessageQueue respq = this->queue;
@@ -103,7 +104,7 @@ httpWriterWrite(void * _this, int fd) @@ -103,7 +104,7 @@ httpWriterWrite(void * _this, int fd)
103 * write 104 * write
104 */ 105 */
105 { 106 {
106 - ssize_t written = cbufWrite(this->buffer, fd); 107 + ssize_t written = cbufWrite(this->buffer, st);
107 108
108 if (0 <= written) { 109 if (0 <= written) {
109 this->written += written; 110 this->written += written;
@@ -24,10 +24,7 @@ @@ -24,10 +24,7 @@
24 #include <stdlib.h> 24 #include <stdlib.h>
25 25
26 #include "interface.h" 26 #include "interface.h"
27 -  
28 -#ifndef TRUE  
29 -#define TRUE 1  
30 -#endif // TRUE 27 +#include "commons.h"
31 28
32 static 29 static
33 inline 30 inline
@@ -22,6 +22,7 @@ @@ -22,6 +22,7 @@
22 22
23 #include "class.h" 23 #include "class.h"
24 #include "interface/stream_reader.h" 24 #include "interface/stream_reader.h"
  25 +#include "stream.h"
25 26
26 const struct interface i_StreamReader = { 27 const struct interface i_StreamReader = {
27 "streamReader", 28 "streamReader",
@@ -29,11 +30,11 @@ const struct interface i_StreamReader = { @@ -29,11 +30,11 @@ const struct interface i_StreamReader = {
29 }; 30 };
30 31
31 ssize_t 32 ssize_t
32 -streamReaderRead(void * object, int fd) 33 +streamReaderRead(void * object, Stream st)
33 { 34 {
34 ssize_t ret; 35 ssize_t ret;
35 36
36 - RETCALL(object, StreamReader, read, ret, fd); 37 + RETCALL(object, StreamReader, read, ret, st);
37 38
38 return ret; 39 return ret;
39 } 40 }
@@ -22,6 +22,7 @@ @@ -22,6 +22,7 @@
22 22
23 #include "class.h" 23 #include "class.h"
24 #include "interface/stream_writer.h" 24 #include "interface/stream_writer.h"
  25 +#include "stream.h"
25 26
26 const struct interface i_StreamWriter = { 27 const struct interface i_StreamWriter = {
27 "streamWriter", 28 "streamWriter",
@@ -29,11 +30,11 @@ const struct interface i_StreamWriter = { @@ -29,11 +30,11 @@ const struct interface i_StreamWriter = {
29 }; 30 };
30 31
31 ssize_t 32 ssize_t
32 -streamWriterWrite(void * object, int fd) 33 +streamWriterWrite(void * object, Stream st)
33 { 34 {
34 ssize_t ret; 35 ssize_t ret;
35 36
36 - RETCALL(object, StreamWriter, write, ret, fd); 37 + RETCALL(object, StreamWriter, write, ret, st);
37 38
38 return ret; 39 return ret;
39 } 40 }
@@ -33,6 +33,7 @@ serverCloseConn(Server this, unsigned int i) @@ -33,6 +33,7 @@ serverCloseConn(Server this, unsigned int i)
33 33
34 delete((this->conns)[fd].sock); 34 delete((this->conns)[fd].sock);
35 delete((this->conns)[fd].worker); 35 delete((this->conns)[fd].worker);
  36 + delete((this->conns)[fd].stream);
36 37
37 memset(&(this->fds[i]), 0, sizeof(struct pollfd)); 38 memset(&(this->fds[i]), 0, sizeof(struct pollfd));
38 } 39 }
@@ -28,6 +28,7 @@ @@ -28,6 +28,7 @@
28 #include "server.h" 28 #include "server.h"
29 #include "interface/class.h" 29 #include "interface/class.h"
30 #include "interface/logger.h" 30 #include "interface/logger.h"
  31 +#include "stream.h"
31 32
32 int 33 int
33 serverHandleAccept(Server this) 34 serverHandleAccept(Server this)
@@ -47,6 +48,7 @@ serverHandleAccept(Server this) @@ -47,6 +48,7 @@ serverHandleAccept(Server this)
47 48
48 // clone worker 49 // clone worker
49 (this->conns)[acc->handle].worker = clone(this->worker); 50 (this->conns)[acc->handle].worker = clone(this->worker);
  51 + (this->conns)[acc->handle].stream = new(Stream, STREAM_FD, acc->handle);
50 52
51 (this->fds)[this->nfds].fd = acc->handle; 53 (this->fds)[this->nfds].fd = acc->handle;
52 (this->fds)[this->nfds].events = POLLIN; 54 (this->fds)[this->nfds].events = POLLIN;
@@ -40,7 +40,10 @@ serverRead(Server this, unsigned int i) @@ -40,7 +40,10 @@ serverRead(Server this, unsigned int i)
40 return -1; 40 return -1;
41 } 41 }
42 42
43 - switch ((size = streamReaderRead((this->conns)[fd].worker, fd))) { 43 + switch ((size = streamReaderRead(
  44 + (this->conns)[fd].worker,
  45 + (this->conns)[fd].stream)))
  46 + {
44 case -2: 47 case -2:
45 /** 48 /**
46 * normal close: this must be mapped to -2 within the 49 * normal close: this must be mapped to -2 within the
@@ -40,7 +40,10 @@ serverWrite(Server this, unsigned int i) @@ -40,7 +40,10 @@ serverWrite(Server this, unsigned int i)
40 return -1; 40 return -1;
41 } 41 }
42 42
43 - remaining = streamWriterWrite((this->conns)[fd].worker, fd); 43 + remaining = streamWriterWrite(
  44 + (this->conns)[fd].worker,
  45 + (this->conns)[fd].stream);
  46 +
44 switch(remaining) { 47 switch(remaining) {
45 case -1: 48 case -1:
46 serverCloseConn(this, i); 49 serverCloseConn(this, i);
  1 +#include <stdarg.h>
  2 +#include <openssl/ssl.h>
  3 +
  4 +#include "class.h"
  5 +#include "interface/class.h"
  6 +#include "stream.h"
  7 +
  8 +
  9 +static
  10 +int
  11 +streamCtor(void * _this, va_list * params)
  12 +{
  13 + Stream this = _this;
  14 + this->type = va_arg(* params, StreamHandleType);
  15 +
  16 + switch(this->type) {
  17 + case STREAM_FD:
  18 + (this->handle).fd = va_arg(* params, int);
  19 + break;
  20 +
  21 + case STREAM_SSL:
  22 + (this->handle).ssl = va_arg(* params, SSL*);
  23 + break;
  24 +
  25 + default:
  26 + return -1;
  27 + }
  28 +
  29 + return 0;
  30 +}
  31 +
  32 +static
  33 +void
  34 +streamDtor(void * _this)
  35 +{
  36 +}
  37 +
  38 +INIT_IFACE(Class, streamCtor, streamDtor, NULL);
  39 +CREATE_CLASS(Stream, NULL, IFACE(Class));
  40 +
  41 +// vim: set ts=4 sw=4:
  1 +#include <openssl/ssl.h>
  2 +#include <unistd.h>
  3 +
  4 +#include "stream.h"
  5 +
  6 +ssize_t
  7 +streamRead(Stream this, void * buf, size_t count)
  8 +{
  9 + ssize_t done;
  10 +
  11 + switch(this->type) {
  12 + case STREAM_FD:
  13 + done = read((this->handle).fd, buf, count);
  14 + break;
  15 +
  16 + case STREAM_SSL:
  17 + done = SSL_read((this->handle).ssl, buf, count);
  18 + break;
  19 +
  20 + default:
  21 + break;
  22 + }
  23 +
  24 + return done;
  25 +}
  26 +
  27 +// vim: set ts=4 sw=4:
  1 +#include <openssl/ssl.h>
  2 +#include <unistd.h>
  3 +
  4 +#include "stream.h"
  5 +
  6 +ssize_t
  7 +streamWrite(Stream this, void * buf, size_t count)
  8 +{
  9 + ssize_t done;
  10 +
  11 + switch(this->type) {
  12 + case STREAM_FD:
  13 + done = write((this->handle).fd, buf, count);
  14 + break;
  15 +
  16 + case STREAM_SSL:
  17 + done = SSL_write((this->handle).ssl, buf, count);
  18 + break;
  19 +
  20 + default:
  21 + break;
  22 + }
  23 +
  24 + return done;
  25 +}
  26 +
  27 +// vim: set ts=4 sw=4:
@@ -8,13 +8,7 @@ @@ -8,13 +8,7 @@
8 8
9 #include "interface/class.h" 9 #include "interface/class.h"
10 10
11 -#ifndef TRUE  
12 -#define TRUE 1  
13 -#endif  
14 -  
15 -#ifndef FALSE  
16 -#define FALSE 0  
17 -#endif 11 +#include "commons.h"
18 12
19 char 13 char
20 isHttpVersion(const char * str, size_t len) 14 isHttpVersion(const char * str, size_t len)
@@ -33,6 +33,7 @@ @@ -33,6 +33,7 @@
33 #include <sys/param.h> 33 #include <sys/param.h>
34 #include <sys/stat.h> 34 #include <sys/stat.h>
35 #include <sys/mman.h> 35 #include <sys/mman.h>
  36 +#include <errno.h>
36 37
37 #include "server.h" 38 #include "server.h"
38 #include "logger.h" 39 #include "logger.h"
@@ -133,7 +134,7 @@ main() @@ -133,7 +134,7 @@ main()
133 shm_unlink("/fooshm"); 134 shm_unlink("/fooshm");
134 close(shm); 135 close(shm);
135 136
136 - logger = new(LoggerSyslog, LOGGER_ERR); 137 + logger = new(LoggerStderr, LOGGER_DEBUG);
137 worker = new(HttpWorker, "testserver", value); 138 worker = new(HttpWorker, "testserver", value);
138 server = new(Server, logger, worker, 11212, SOMAXCONN); 139 server = new(Server, logger, worker, 11212, SOMAXCONN);
139 140
@@ -149,21 +150,34 @@ main() @@ -149,21 +150,34 @@ main()
149 do { 150 do {
150 pid_t w; 151 pid_t w;
151 152
152 - w = waitpid(pid, &status, WUNTRACED | WCONTINUED); 153 + w = waitpid(pid, &status, 0);
153 154
154 - if (w == -1) {  
155 - perror("waitpid");  
156 - exit(EXIT_FAILURE); 155 + while (w == -1) {
  156 + switch(errno) {
  157 + case EINTR: w = waitpid(pid, &status, 0);
  158 + break;
  159 + case ECHILD: perror("no child");
  160 + // DROP THROUGH
  161 + default: w = 0;
  162 + }
157 } 163 }
158 164
159 - if (WIFEXITED(status)) {  
160 - printf("exited, status=%d\n", WEXITSTATUS(status));  
161 - } else if (WIFSIGNALED(status)) {  
162 - printf("killed by signal %d\n", WTERMSIG(status));  
163 - } else if (WIFSTOPPED(status)) {  
164 - printf("stopped by signal %d\n", WSTOPSIG(status));  
165 - } else if (WIFCONTINUED(status)) {  
166 - printf("continued\n"); 165 + if (0 < w) {
  166 + if (WIFEXITED(status)) {
  167 + loggerLog(logger, LOGGER_INFO,
  168 + "child exited, status=%d\n",
  169 + WEXITSTATUS(status));
  170 + } else if (WIFSIGNALED(status)) {
  171 + loggerLog(logger, LOGGER_INFO,
  172 + "killed by signal %d\n",
  173 + WTERMSIG(status));
  174 + } else if (WIFSTOPPED(status)) {
  175 + loggerLog(logger, LOGGER_INFO,
  176 + "stopped by signal %d\n",
  177 + WSTOPSIG(status));
  178 + } else if (WIFCONTINUED(status)) {
  179 + loggerLog(logger, LOGGER_INFO, "continued\n");
  180 + }
167 } 181 }
168 } while (!WIFEXITED(status) && !WIFSIGNALED(status)); 182 } while (!WIFEXITED(status) && !WIFSIGNALED(status));
169 183
Please register or login to post a comment