]>
Commit | Line | Data |
---|---|---|
7460295f | 1 | // http.h -- Library for implementing embedded custom HTTP servers |
bc83b450 | 2 | // Copyright (C) 2008-2010 Markus Gutschke <markus@shellinabox.com> |
7460295f MG |
3 | // |
4 | // This program is free software; you can redistribute it and/or modify | |
5 | // it under the terms of the GNU General Public License version 2 as | |
6 | // published by the Free Software Foundation. | |
7 | // | |
8 | // This program is distributed in the hope that it will be useful, | |
9 | // but WITHOUT ANY WARRANTY; without even the implied warranty of | |
10 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
11 | // GNU General Public License for more details. | |
12 | // | |
13 | // You should have received a copy of the GNU General Public License along | |
14 | // with this program; if not, write to the Free Software Foundation, Inc., | |
15 | // 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. | |
16 | // | |
17 | // In addition to these license terms, the author grants the following | |
18 | // additional rights: | |
19 | // | |
20 | // If you modify this program, or any covered work, by linking or | |
21 | // combining it with the OpenSSL project's OpenSSL library (or a | |
22 | // modified version of that library), containing parts covered by the | |
23 | // terms of the OpenSSL or SSLeay licenses, the author | |
24 | // grants you additional permission to convey the resulting work. | |
25 | // Corresponding Source for a non-source form of such a combination | |
26 | // shall include the source code for the parts of OpenSSL used as well | |
27 | // as that of the covered work. | |
28 | // | |
29 | // You may at your option choose to remove this additional permission from | |
30 | // the work, or from any part of it. | |
31 | // | |
32 | // It is possible to build this program in a way that it loads OpenSSL | |
33 | // libraries at run-time. If doing so, the following notices are required | |
34 | // by the OpenSSL and SSLeay licenses: | |
35 | // | |
36 | // This product includes software developed by the OpenSSL Project | |
37 | // for use in the OpenSSL Toolkit. (http://www.openssl.org/) | |
38 | // | |
39 | // This product includes cryptographic software written by Eric Young | |
40 | // (eay@cryptsoft.com) | |
41 | // | |
42 | // | |
43 | // The most up-to-date version of this program is always available from | |
44 | // http://shellinabox.com | |
45 | ||
46 | #ifndef LIB_HTTP_H__ | |
47 | #define LIB_HTTP_H__ | |
48 | ||
49 | #include <errno.h> | |
50 | #include <stdarg.h> | |
51 | #include <time.h> | |
52 | ||
53 | #define HTTP_DONE 0 | |
54 | #define HTTP_ERROR 1 | |
55 | #define HTTP_READ_MORE 2 | |
56 | #define HTTP_SUSPEND 3 | |
57 | #define HTTP_PARTIAL_REPLY 4 | |
58 | ||
bc83b450 MG |
59 | #define WS_START_OF_FRAME 0x0100 |
60 | #define WS_END_OF_FRAME 0x0200 | |
61 | #define WS_CONNECTION_OPENED 0xFF00 | |
62 | #define WS_CONNECTION_CLOSED 0x7F00 | |
63 | ||
9b850878 | 64 | #define NO_MSG "\001" |
bc83b450 | 65 | #define BINARY_MSG "\001%d%p" |
9b850878 | 66 | |
7460295f MG |
67 | #define NOINTR(x) ({ int i__; while ((i__ = (x)) < 0 && errno == EINTR); i__;}) |
68 | ||
69 | typedef struct HashMap HashMap; | |
70 | typedef struct HttpConnection HttpConnection; | |
71 | typedef struct ServerConnection ServerConnection; | |
72 | typedef struct Server Server; | |
73 | typedef struct URL URL; | |
74 | ||
53b27f78 MG |
75 | Server *newCGIServer(int localhostOnly, int portMin, int portMax, int timeout); |
76 | Server *newServer(int localhostOnly, int port); | |
7460295f | 77 | void deleteServer(Server *server); |
d1edcc0e MG |
78 | int serverGetListeningPort(Server *server); |
79 | int serverGetFd(Server *server); | |
7460295f MG |
80 | void serverRegisterHttpHandler(Server *server, const char *url, |
81 | int (*handler)(HttpConnection *, void *, | |
82 | const char *, int), void *arg); | |
83 | void serverRegisterStreamingHttpHandler(Server *server, const char *url, | |
84 | int (*handler)(HttpConnection *, void *), | |
85 | void *arg); | |
bc83b450 MG |
86 | void serverRegisterWebSocketHandler(Server *server, const char *url, |
87 | int (*handler)(HttpConnection *, void *, int, const char *, int), | |
88 | void *arg); | |
7460295f MG |
89 | ServerConnection *serverAddConnection(Server *server, int fd, |
90 | int (*handleConnection)(ServerConnection *, | |
91 | void *arg, short *events, | |
92 | short revents), | |
93 | void (*destroyConnection)(void *arg), | |
94 | void *arg); | |
95 | void serverDeleteConnection(Server *server, int fd); | |
96 | void serverSetTimeout(ServerConnection *connection, time_t timeout); | |
97 | time_t serverGetTimeout(ServerConnection *connection); | |
98 | ServerConnection *serverGetConnection(Server *server, ServerConnection *hint, | |
99 | int fd); | |
100 | short serverConnectionSetEvents(Server *server, ServerConnection *connection, | |
101 | short events); | |
102 | void serverExitLoop(Server *server, int exitAll); | |
103 | void serverLoop(Server *server); | |
104 | int serverSupportsSSL(); | |
105 | void serverEnableSSL(Server *server, int flag); | |
106 | void serverSetCertificate(Server *server, const char *filename, | |
107 | int autoGenerateMissing); | |
dc6575f2 | 108 | void serverSetCertificateFd(Server *server, int fd); |
7460295f MG |
109 | void serverSetNumericHosts(Server *server, int numericHosts); |
110 | ||
111 | void httpTransfer(HttpConnection *http, char *msg, int len); | |
112 | void httpTransferPartialReply(HttpConnection *http, char *msg, int len); | |
113 | void httpSetCallback(HttpConnection *http, | |
114 | int (*callback)(HttpConnection *, void *, | |
115 | const char *, int), void *arg); | |
116 | void *httpGetPrivate(HttpConnection *http); | |
117 | void *httpSetPrivate(HttpConnection *http, void *private); | |
118 | void httpSendReply(HttpConnection *http, int code, | |
119 | const char *msg, const char *fmt, ...) | |
120 | __attribute__((format(printf, 4, 5))); | |
bc83b450 MG |
121 | void httpSendWebSocketTextMsg(HttpConnection *http, int type, const char *fmt, |
122 | ...) __attribute__((format(printf, 3, 4))); | |
123 | void httpSendWebSocketBinaryMsg(HttpConnection *http, int type, | |
124 | const void *buf, int len); | |
7460295f MG |
125 | void httpExitLoop(HttpConnection *http, int exitAll); |
126 | Server *httpGetServer(const HttpConnection *http); | |
127 | ServerConnection *httpGetServerConnection(const HttpConnection *); | |
128 | int httpGetFd(const HttpConnection *http); | |
129 | const char *httpGetPeerName(const HttpConnection *http); | |
130 | const char *httpGetMethod(const HttpConnection *http); | |
131 | const char *httpGetVersion(const HttpConnection *http); | |
132 | const HashMap *httpGetHeaders(const HttpConnection *http); | |
133 | URL *newURL(const HttpConnection *http, const char *buf, int len); | |
134 | void deleteURL(URL *url); | |
135 | const char *urlGetProtocol(URL *url); | |
136 | const char *urlGetUser(URL *url); | |
137 | const char *urlGetPassword(URL *url); | |
138 | const char *urlGetHost(URL *url); | |
139 | int urlGetPort(URL *url); | |
140 | const char *urlGetPath(URL *url); | |
141 | const char *urlGetPathInfo(URL *url); | |
142 | const char *urlGetQuery(URL *url); | |
143 | const char *urlGetAnchor(URL *url); | |
90cccba6 | 144 | const char *urlGetURL(URL *url); |
7460295f MG |
145 | const HashMap *urlGetArgs(URL *url); |
146 | HashMap *newHashMap(void (*destructor)(void *arg, char *key, char *value), | |
147 | void *arg); | |
148 | void deleteHashMap(HashMap *hashmap); | |
149 | const void *addToHashMap(HashMap *hashmap, const char *key, const char *value); | |
150 | void deleteFromHashMap(HashMap *hashmap, const char *key); | |
151 | char **getRefFromHashMap(const HashMap *hashmap, const char *key); | |
152 | const char *getFromHashMap(const HashMap *hashmap, const char *key); | |
153 | void iterateOverHashMap(struct HashMap *hashmap, | |
154 | int (*fnc)(void *arg, const char *key, char **value), | |
155 | void *arg); | |
156 | int getHashmapSize(const HashMap *hashmap); | |
157 | ||
158 | #endif /* LIB_HTTP_H__ */ |