forked from avih/miniweb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhttpapi.h
432 lines (377 loc) · 12.3 KB
/
httpapi.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
////////////////////////////////////////////////////////////////////////
//
// httpapi.h
//
// External API header file for http protocol
//
///////////////////////////////////////////////////////////////////////
#ifndef _HTTPAPI_H_
#define _HTTPAPI_H_
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <time.h>
#include <sys/types.h>
#include "httppil.h"
#define VER_MAJOR 0
#define VER_MINOR 8
#ifndef min
#define min(x,y) (x>y?y:x)
#endif
#ifdef HTTP_DEBUG
#define DBG(...) cc_fprintf(stderr, __VA_ARGS__)
#else
#define DBG(...) /* no-op */
#endif
#define LOG_ERR 1
#define ASSERT
#define GETDWORD(ptrData) (*(DWORD*)(ptrData))
#define SETDWORD(ptrData,data) (*(DWORD*)(ptrData)=data)
#define GETWORD(ptrData) (*(WORD*)(ptrData))
#define SETWORD(ptrData,data) (*(WORD*)(ptrData)=data)
#ifndef BIG_ENDINE
#define DEFDWORD(char1,char2,char3,char4) ((char1)+((char2)<<8)+((char3)<<16)+((char4)<<24))
#define DEFWORD(char1,char2) (char1+(char2<<8))
#else
#define DEFDWORD(char1,char2,char3,char4) ((char4)+((char3)<<8)+((char2)<<16)+((char1)<<24))
#define DEFWORD(char1,char2) (char2+(char1<<8))
#endif
#include "win32/win_compat.h"
///////////////////////////////////////////////////////////////////////
// Public definitions
///////////////////////////////////////////////////////////////////////
#ifdef __cplusplus
extern "C" {
#endif
// file types
typedef enum {
HTTPFILETYPE_UNKNOWN = 0,
HTTPFILETYPE_HTML,
HTTPFILETYPE_XML,
HTTPFILETYPE_TEXT,
HTTPFILETYPE_XUL,
HTTPFILETYPE_CSS,
HTTPFILETYPE_JS,
HTTPFILETYPE_PNG,
HTTPFILETYPE_JPEG,
HTTPFILETYPE_GIF,
HTTPFILETYPE_SWF,
HTTPFILETYPE_MPA,
HTTPFILETYPE_MPEG,
HTTPFILETYPE_AVI,
HTTPFILETYPE_MP4,
HTTPFILETYPE_MOV,
HTTPFILETYPE_264,
HTTPFILETYPE_FLV,
HTTPFILETYPE_TS,
HTTPFILETYPE_3GP,
HTTPFILETYPE_ASF,
HTTPFILETYPE_OCTET,
HTTPFILETYPE_STREAM,
HTTPFILETYPE_M3U8,
HTTPFILETYPE_SDP,
HTTPFILETYPE_HEX,
HTTPFILETYPE_SVG,
} HttpFileType;
#define MAXPOSTPARAMS 50
#define MAXPOSTREDIRECTFILENAME (200)
/////////////////////////////////////////////////////////////////////////////
// typedefs
/////////////////////////////////////////////////////////////////////////////
typedef struct _tagPostParam {
// char* pchPageName;
struct {
char* pchParamName;
char* pchParamValue;
} stParams[MAXPOSTPARAMS];
void *httpParam;
int iNumParams;
char *pchPath;
} PostParam;
// multipart file upload post (per socket) structure
typedef struct {
char pchBoundaryValue[80];
OCTET oFileuploadStatus;
size_t writeLocation;
PostParam pp;
char pchFilename[MAX_PATH];
int fd;
} HttpMultipart;
typedef struct _tagSubstParam {
char* pchParamName;
char* pchParamValue; // returned
int iMaxValueBytes;
} SubstParam;
#define FLAG_REQUEST_GET 0x1
#define FLAG_REQUEST_POST 0x2
#ifdef ENABLE_RTSP
#define FLAG_REQUEST_OPTIONS 0x4
#define FLAG_REQUEST_DESCRIBE 0x8
#define FLAG_REQUEST_SETUP 0x10
#define FLAG_REQUEST_PLAY 0x20
#define FLAG_REQUEST_TEARDOWN 0x40
#endif
#define FLAG_HEADER_SENT 0x80
#define FLAG_CONN_CLOSE 0x100
#define FLAG_SUBST 0x200
#define FLAG_AUTHENTICATION 0x400
#define FLAG_MORE_CONTENT 0x800
#define FLAG_TO_FREE 0x1000
#define FLAG_CHUNK 0x2000
#define FLAG_CLOSE_CALLBACK 0x4000
#define FLAG_DATA_FILE 0x10000
#define FLAG_DATA_RAW 0x20000
#define FLAG_DATA_FD 0x40000
#define FLAG_DATA_REDIRECT 0x80000
#define FLAG_DATA_STREAM 0x100000
#define FLAG_CUSTOM_HEADER 0x200000
#define FLAG_MULTIPART 0x400000
#define FLAG_RECEIVING 0x80000000
#define FLAG_SENDING 0x40000000
#define SETFLAG(hs,bit) (hs->flags|=(bit));
#define CLRFLAG(hs,bit) (hs->flags&=~(bit));
#define ISFLAGSET(hs,bit) (hs->flags&(bit))
typedef union {
unsigned long laddr;
unsigned short saddr[2];
unsigned char caddr[4];
} IPADDR;
typedef struct {
int iHttpVer;
size_t startByte;
char *pucPath;
const char *pucReferer;
char* pucHost;
int headerSize;
char* pucPayload;
size_t payloadSize;
int iCSeq;
const char* pucTransport;
const char* pucAuthInfo;
} HttpRequest;
typedef struct {
int statusCode;
int headerBytes;
int sentBytes;
cc_off_t contentLength;
HttpFileType fileType;
} HttpResponse;
typedef struct {
char *name;
char *value;
} HttpVariables;
// Callback function protos
typedef int (*PFNPOSTCALLBACK)(PostParam*);
typedef int (*PFNSUBSTCALLBACK)(SubstParam*);
typedef int (*PFNFILEUPLOADCALLBACK)(HttpMultipart*, OCTET*, size_t);
typedef int (*PFNIDLECALLBACK)(void* hp);
typedef enum {
MW_INIT = 0,
MW_UNINIT,
MW_PARSE_ARGS,
} MW_EVENT;
typedef int (*PFNEVENTHANDLER)(MW_EVENT msg, void* handler, void* hp);
typedef struct {
time_t startTime;
WORD clientCount;
WORD clientCountMax;
size_t reqCount;
size_t fileSentCount;
size_t fileSentBytes;
int varSubstCount;
int urlProcessCount;
int timeOutCount;
int authFailCount;
int fileUploadCount;
} HttpStats;
#define HTTP_BUFFER_SIZE (128*1024 /*bytes*/)
#define HTTP_MAX_CLIENTS 32
// per connection/socket structure
typedef struct _HttpSocket{
SOCKET socket;
IPADDR ipAddr;
HttpRequest request;
HttpResponse response;
char *pucData;
int bufferSize; // the size of buffer pucData pointing to
int dataLength;
#ifdef WINCE
HANDLE fd;
#else
int fd;
#endif
unsigned int flags;
void* handler; // http handler function address
void* ptr;
time_t tmAcceptTime;
time_t tmExpirationTime;
DWORD dwResumeTick;
int iRequestCount;
char* mimeType;
HttpMultipart* pxMP;
char* buffer;
} HttpSocket;
typedef struct {
void* hp;
HttpSocket* hs;
const char *pucRequest;
HttpVariables* pxVars;
int iVarCount;
char *pucHeader;
char *pucBuffer;
char *pucPayload;
int dataBytes;
int contentBytes;
HttpFileType fileType;
void *p_sys;
} UrlHandlerParam;
typedef int (*PFNURLCALLBACK)(UrlHandlerParam*);
typedef struct {
const char* pchUrlPrefix;
PFNURLCALLBACK pfnUrlHandler;
PFNEVENTHANDLER pfnEventHandler;
void *p_sys;
} UrlHandler;
#define AUTH_NO_NEED (0)
#define AUTH_SUCCESSED (1)
#define AUTH_REQUIRED (2)
#define AUTH_FAILED (-1)
#define MAX_AUTH_INFO_LEN 64
typedef struct {
const char* pchUrlPrefix;
const char* pchUsername;
const char* pchPassword;
const char* pchOtherInfo;
char pchAuthString[MAX_AUTH_INFO_LEN];
} AuthHandler;
#ifndef DISABLE_VIRTUAL_PATH
typedef struct {
char* pchUrlPrefix;
char pchLocalRealPath[MAX_PATH];
} VirtPathHandler;
#endif
#define FLAG_DIR_LISTING 1
#define FLAG_DISABLE_RANGE 2
typedef void (*mwShutdownCallback)(void); // void onShutdown(void) { ... }
typedef struct _httpParam {
HttpSocket* hsSocketQueue; /* socket queue*/
int maxClients;
int maxClientsPerIP;
int bKillWebserver;
int bKillingWebserver;
int bWebserverRunning;
unsigned int flags;
SOCKET listenSocket;
int httpPort;
int socketRcvBufSize; /* socket receive buffer size in KB */
char pchWebPath[MAX_PATH];
UrlHandler *pxUrlHandler; /* pointer to URL handler array */
AuthHandler *pxAuthHandler; /* pointer to authorization handler array */
#ifndef DISABLE_VIRTUAL_PATH
VirtPathHandler *pxVirtPathHandler;
#endif
// substitution callback
PFNSUBSTCALLBACK pfnSubst;
// post callbacks
PFNFILEUPLOADCALLBACK pfnFileUpload;
PFNPOSTCALLBACK pfnPost;
// idle callback
PFNIDLECALLBACK pfnIdleCallback;
// misc
DWORD dwAuthenticatedNode;
time_t tmAuthExpireTime;
time_t tmSocketExpireTime;
int maxDownloadSpeed; /* maximum download speed in KB/s */
HttpStats stats;
u_long hlBindIP;
void* szctx;
mwShutdownCallback postHttpLoop; // only the shutdown handler should set this
} HttpParam;
typedef struct {
const char* pchRootPath;
const char* pchHttpPath;
char cFilePath[MAX_PATH];
char* pchExt;
int isDirRequest;
} HttpFilePath;
///////////////////////////////////////////////////////////////////////
// Return codes
///////////////////////////////////////////////////////////////////////
// for post callback
#define WEBPOST_OK (0)
#define WEBPOST_AUTHENTICATED (1)
#define WEBPOST_NOTAUTHENTICATED (2)
#define WEBPOST_AUTHENTICATIONON (3)
#define WEBPOST_AUTHENTICATIONOFF (4)
// for multipart file uploads
#define HTTPUPLOAD_MORECHUNKS (0)
#define HTTPUPLOAD_FIRSTCHUNK (1)
#define HTTPUPLOAD_LASTCHUNK (2)
///////////////////////////////////////////////////////////////////////
// Public functions
///////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////
// mwInitParam. Init the context structure with default values
///////////////////////////////////////////////////////////////////////
void mwInitParam(HttpParam* hp);
///////////////////////////////////////////////////////////////////////
// mwServerStart. Startup the webserver
///////////////////////////////////////////////////////////////////////
int mwServerStart(HttpParam* hp);
///////////////////////////////////////////////////////////////////////
// mwHttpLoop. Enter webserver loop
///////////////////////////////////////////////////////////////////////
void* mwHttpLoop(void* _hp);
///////////////////////////////////////////////////////////////////////
// mwServerShutdown. Shutdown the webserver (closes connections and
// releases resources)
// Note: this funtion cannot guarantee successful shutdown, and it
// operates in one of two modes:
// 1. If cb is provided, it indicates that the caller is running on
// the same thread as the server. At this case, it only signals
// the server to shutdown, ignores timeout_ms, will always return 0,
// and will call cb after the shutdown completes. It's up to the
// caller to handle timeout in case the callback is never invoked.
// 2. if cb is not provided, the function assumes that it runs on a
// different thread than the server, will signal the server to
// shutdown, and will wait timeout_ms to let it complete. It will
// return 0 if the shutdown completed successfully, or negative
// value if the server didn't shutdown within the timeout.
//
// Calling this function more than once will always return -1 for
// redundant calls (this resets when starting the server again).
///////////////////////////////////////////////////////////////////////
int mwServerShutdown(HttpParam*, mwShutdownCallback cb, unsigned int timeout_ms);
///////////////////////////////////////////////////////////////////////
// mwSetRcvBufSize. Change the TCP windows size of acceped sockets
///////////////////////////////////////////////////////////////////////
int mwSetRcvBufSize(WORD wSize);
///////////////////////////////////////////////////////////////////////
// mwPostRegister. Specify the callback to be called when a POST is
// recevied.
///////////////////////////////////////////////////////////////////////
PFNPOSTCALLBACK mwPostRegister(HttpParam *httpParam, PFNPOSTCALLBACK);
///////////////////////////////////////////////////////////////////////
// mwFileUploadRegister. Specify the callback to be called whenever the
// server has the next data chunk available from a multipart file upload.
///////////////////////////////////////////////////////////////////////
PFNFILEUPLOADCALLBACK mwFileUploadRegister(HttpParam *httpParam, PFNFILEUPLOADCALLBACK);
///////////////////////////////////////////////////////////////////////
// Default subst, post and file-upload callback processing
///////////////////////////////////////////////////////////////////////
int DefaultWebSubstCallback(SubstParam* sp);
int DefaultWebPostCallback(PostParam* pp);
int DefaultWebFileUploadCallback(HttpMultipart *pxMP, OCTET *poData, size_t dataChunkSize);
int mwGetHttpDateTime(time_t tm, char *buf, int bufsize);
int mwGetLocalFileName(HttpFilePath* hfp);
char* mwGetVarValue(HttpVariables* vars, const char *varname, const char *defval);
int mwGetVarValueInt(HttpVariables* vars, const char *varname, int defval);
unsigned int mwGetVarValueHex(HttpVariables* vars, const char *varname, unsigned int defval);
int mwParseQueryString(UrlHandlerParam* up);
int mwGetContentType(const char *pchExtname);
void mwDecodeString(char* s);
#ifdef __cplusplus
}
#endif
#endif // _HTTPAPI_H
////////////////////////// END OF FILE ////////////////////////////////