... | ... |
@@ -1,36 +1,147 @@ |
1 | 1 |
/* |
2 |
- * webkernel.v |
|
2 |
+ * webkernel.c |
|
3 | 3 |
* |
4 | 4 |
* A small embeddable web server. |
5 | 5 |
* |
6 |
- * 21/01/2014 Creation. |
|
7 |
- * |
|
8 | 6 |
* Author: Dario Rodriguez dario@softhome.net |
9 | 7 |
* This library is licensed on the terms of the GNU LGPL v2+ |
10 | 8 |
*/ |
11 | 9 |
|
12 | 10 |
|
13 |
-typedef struct { |
|
11 |
+#include <stdio.h> |
|
12 |
+#include <stdlib.h> |
|
13 |
+#include <unistd.h> |
|
14 |
+#include <string.h> |
|
15 |
+#include "webkernel.h" |
|
16 |
+ |
|
17 |
+typedef struct _wk { |
|
14 | 18 |
int dummy; |
15 |
-} swk; |
|
19 |
+} _wk; |
|
16 | 20 |
|
17 | 21 |
wk * |
18 |
-wk_init(int port) |
|
22 |
+wk_init(int port, sselect *ssel, void (*callback_event)(/*wk *paramweb,int connid, wk_event event, void *userptr*/), void (*callback_http)(/*wk *paramweb,int connid, wk_uri *uri, void *userptr*/), void (*callback_continuation)(/*wk *paramweb,int connid, wk_uri *uri, void *userptr*/), void *userptr) |
|
19 | 23 |
{ |
24 |
+ _wk *web; |
|
25 |
+ if((web=malloc(sizeof(_wk)))==NULL) |
|
26 |
+ return(NULL); |
|
27 |
+ memset(web,0,sizeof(_wk)); |
|
28 |
+ return((wk *)web); |
|
20 | 29 |
} |
21 | 30 |
|
22 | 31 |
void |
23 |
-wk_free(wk *web) |
|
32 |
+wk_free(wk *paramweb) |
|
33 |
+{ |
|
34 |
+ _wk *web=(_wk *)paramweb; |
|
35 |
+ if(web==NULL) |
|
36 |
+ return; |
|
37 |
+ free(web),web=NULL; |
|
38 |
+} |
|
39 |
+ |
|
40 |
+wk_uri * |
|
41 |
+wk_geturi(wk *paramweb, int connid) |
|
42 |
+{ |
|
43 |
+ |
|
44 |
+} |
|
45 |
+ |
|
46 |
+int |
|
47 |
+wk_service(wk *paramweb) |
|
48 |
+{ |
|
49 |
+ |
|
50 |
+} |
|
51 |
+ |
|
52 |
+int |
|
53 |
+wk_serve_buffer_as_file(wk *paramweb, int connid, void *data, int datalen, const char *mime) |
|
54 |
+{ |
|
55 |
+ |
|
56 |
+} |
|
57 |
+ |
|
58 |
+int |
|
59 |
+wk_serve_file(wk *paramweb, int connid, char *filename, const char *mime) |
|
60 |
+{ |
|
61 |
+ |
|
62 |
+} |
|
63 |
+ |
|
64 |
+int |
|
65 |
+wk_serve_error(wk *paramweb, int connid, wk_error wkerror) |
|
66 |
+{ |
|
67 |
+ |
|
68 |
+} |
|
69 |
+ |
|
70 |
+int |
|
71 |
+wk_writestr(wk *paramweb, int connid, char *str) |
|
72 |
+{ |
|
73 |
+ |
|
74 |
+} |
|
75 |
+ |
|
76 |
+int |
|
77 |
+wk_write(wk *paramweb, int connid, void *data, int datalen) |
|
78 |
+{ |
|
79 |
+ |
|
80 |
+} |
|
81 |
+ |
|
82 |
+int |
|
83 |
+wk_close(wk *paramweb, int connid) |
|
84 |
+{ |
|
85 |
+ |
|
86 |
+} |
|
87 |
+ |
|
88 |
+char * |
|
89 |
+wk_uri_getheader(wk_uri *wkuri, char *header, char *defaultvalue) |
|
90 |
+{ |
|
91 |
+ |
|
92 |
+} |
|
93 |
+ |
|
94 |
+char * |
|
95 |
+wk_uri_getheaderbynum(wk_uri *wkuri, int num) |
|
24 | 96 |
{ |
25 | 97 |
|
26 | 98 |
} |
27 | 99 |
|
28 |
-int wk_select(wk *web, int timemoutms,...); |
|
29 |
-int wk_httpaccept(wk *web, int *httpnum); |
|
30 |
-int wk_httpisrequest(char *line); |
|
31 |
-int wk_httpfromdisk(wk *web, int httpnum,char *firstline); |
|
32 |
-int wk_httpreset(wk *web, int httpnum); |
|
33 |
-int wk_httpclose(wk *web, int httpnum); |
|
34 |
-int wk_iobufget(wk *web); |
|
35 |
-int wk_iobuffree(wk *web, int iobufnum); |
|
100 |
+const char * |
|
101 |
+mime_getdefault(char *filename) |
|
102 |
+{ |
|
103 |
+ struct { |
|
104 |
+ char *ext; |
|
105 |
+ char *mime; |
|
106 |
+ } dotdot[]={{".tar.gz","application/x-tgz"}}, |
|
107 |
+ dot[]={ |
|
108 |
+ {"html","text/html"}, |
|
109 |
+ {"htm","text/html"}, |
|
110 |
+ {"shtml","text/html"}, |
|
111 |
+ {"css","text/css"}, |
|
112 |
+ {"gif","image/gif"}, |
|
113 |
+ {"jpeg","image/jpeg"}, |
|
114 |
+ {"jpg","image/jpeg"}, |
|
115 |
+ {"png","image/png"}, |
|
116 |
+ {"tiff","image/tiff"}, |
|
117 |
+ {"tif","image/tiff"}, |
|
118 |
+ {"bmp","image/x-ms-bmp"}, |
|
119 |
+ {"svg","image/svg+xml"}, |
|
120 |
+ {"svgz","image/svg+xml"}, |
|
121 |
+ {"js","application/x-javascript"}, |
|
122 |
+ {"atom","application/atom+xml"}, |
|
123 |
+ {"txt","text/plain"}, |
|
124 |
+ {"json","application/json"}, |
|
125 |
+ {"pdf","application/pdf"}, |
|
126 |
+ {"zip","application/zip"}, |
|
127 |
+ {"mp3","audio/mpeg"}, |
|
128 |
+ {"wav","audio/x-wav"}, |
|
129 |
+ {"ogg","audio/ogg"}, |
|
130 |
+ {"mp4","video/mp4"}, |
|
131 |
+ {"webm","video/webm"}, |
|
132 |
+ {"avi","video/x-msvideo"}}; |
|
133 |
+#warning TODO |
|
134 |
+} |
|
135 |
+ |
|
136 |
+siobuf * |
|
137 |
+wk_iobufget(wk *paramweb) |
|
138 |
+{ |
|
139 |
+ |
|
140 |
+} |
|
141 |
+ |
|
142 |
+void |
|
143 |
+wk_iobuffree(wk *paramweb, int iobufnum) |
|
144 |
+{ |
|
145 |
+ |
|
146 |
+} |
|
36 | 147 |
|
... | ... |
@@ -5,10 +5,6 @@ |
5 | 5 |
* |
6 | 6 |
* Header file. |
7 | 7 |
* |
8 |
- * History: |
|
9 |
- * 21/01/2014 Creation. |
|
10 |
- * 2/04/2014 wk_fdinit()/_fdcheck(). |
|
11 |
- * |
|
12 | 8 |
* Author: Dario Rodriguez dario@softhome.net |
13 | 9 |
* This library is licensed on the terms of the GNU LGPL v2+ |
14 | 10 |
*/ |
... | ... |
@@ -17,21 +13,51 @@ |
17 | 13 |
#define WEBKERNEL_H |
18 | 14 |
#include <sys/select.h> |
19 | 15 |
#include "iobuf.h" |
16 |
+#include "socklib.h" |
|
20 | 17 |
|
21 | 18 |
typedef void wk; |
19 |
+typedef enum wk_event { |
|
20 |
+ wke_init=0, |
|
21 |
+ wke_fini, |
|
22 |
+ wke_connected, |
|
23 |
+ wke_closed, |
|
24 |
+} wk_event; |
|
25 |
+ |
|
26 |
+typedef struct wk_uri { |
|
27 |
+ char *method; |
|
28 |
+ char *protocol; |
|
29 |
+ char *path; |
|
30 |
+ char *query; |
|
31 |
+ char *headers; |
|
32 |
+ void *uriuserptr; |
|
33 |
+} wk_uri; |
|
22 | 34 |
|
23 |
-wk *wk_init(int port); |
|
35 |
+typedef enum wk_error { |
|
36 |
+ wkerr_notfound=404, |
|
37 |
+ wkerr_internal=500, |
|
38 |
+ wkerr_notimplemented=501, |
|
39 |
+} wk_error; |
|
40 |
+ |
|
41 |
+wk *wk_init(int port, sselect *ssel, void (*callback_event)(/*wk *web,int connid, wk_event event, void *userptr*/), void (*callback_http)(/*wk *web,int connid, wk_uri *uri, void *userptr*/), void (*callback_continuation)(/*wk *web,int connid, wk_uri *uri, void *userptr*/), void *userptr); |
|
24 | 42 |
void wk_free(wk *web); |
25 |
-int wk_fdsetinit(wk *web, int *n, fd_set *readfds, fd_set *writefds, fd_set *exceptfds); /* n, readfds, writefds and execptfds must be already initialized to 0 */ |
|
26 |
-int wk_fdsetcheck(wk *web, fd_set *readfds, fd_set *writefds, fd_set *exceptfds); |
|
27 |
-int wk_select(wk *web, int timemoutms,...); |
|
28 |
-int wk_httpaccept(wk *web, int *httpnum); |
|
29 |
-int wk_httpisrequest(char *line); |
|
30 |
-int wk_httpfromdisk(wk *web, int httpnum,char *firstline); |
|
31 |
-int wk_httpreset(wk *web, int httpnum); |
|
32 |
-int wk_httpclose(wk *web, int httpnum); |
|
43 |
+ |
|
44 |
+wk_uri *wk_geturi(wk *web, int connid); /* for use in callback_event() when wke_closed */ |
|
45 |
+ |
|
46 |
+int wk_service(wk *web); /* To be called after sselelect_wait() but before sselect_getXXX() */ |
|
47 |
+ |
|
48 |
+int wk_serve_buffer_as_file(wk *web, int connid, void *data, int datalen, const char *mime); |
|
49 |
+int wk_serve_file(wk *web, int connid, char *filename, const char *mime); |
|
50 |
+int wk_serve_error(wk *web, int connid, wk_error wkerror); |
|
51 |
+int wk_writestr(wk *web, int connid, char *str); |
|
52 |
+int wk_write(wk *web, int connid, void *data, int datalen); |
|
53 |
+int wk_close(wk *web, int connid); |
|
54 |
+ |
|
55 |
+char *wk_uri_getheader(wk_uri *wkuri, char *header, char *defaultvalue); |
|
56 |
+char *wk_uri_getheaderbynum(wk_uri *wkuri, int num); |
|
57 |
+ |
|
58 |
+const char *mime_getdefault(char *filename); |
|
33 | 59 |
|
34 | 60 |
siobuf *wk_iobufget(wk *web); |
35 |
-wk_iobuffree(wk *web, int iobufnum); |
|
61 |
+void wk_iobuffree(wk *web, int iobufnum); |
|
36 | 62 |
|
37 | 63 |
#endif |