-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathhttpapi.h
More file actions
333 lines (284 loc) · 8.86 KB
/
httpapi.h
File metadata and controls
333 lines (284 loc) · 8.86 KB
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
///////////////////////////////////////////////////////////////////////
//
// 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 <fcntl.h>
#include <time.h>
#define VER_MAJOR 0
#define VER_MINOR 8
#ifndef min
#define min(x,y) (x>y?y:x)
#endif
#ifndef NODEBUG
#define DBG printf
#else
#define DBG
#endif
#define LOG_ERR 1
#ifdef WIN32
#ifndef pthread_t
#endif
#endif
#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
///////////////////////////////////////////////////////////////////////
// Public definitions
///////////////////////////////////////////////////////////////////////
#ifdef __cplusplus
extern "C" {
#endif
// file types
typedef enum {
HTTPFILETYPE_UNKNOWN = 0,
HTTPFILETYPE_HTML,
HTTPFILETYPE_XML,
HTTPFILETYPE_TEXT,
HTTPFILETYPE_CSS,
HTTPFILETYPE_PNG,
HTTPFILETYPE_JPEG,
HTTPFILETYPE_GIF,
HTTPFILETYPE_SWF,
HTTPFILETYPE_MPA,
HTTPFILETYPE_MPEG,
HTTPFILETYPE_AVI,
HTTPFILETYPE_MP4,
HTTPFILETYPE_MOV,
HTTPFILETYPE_JS,
HTTPFILETYPE_OCTET,
HTTPFILETYPE_STREAM,
} HttpFileType;
#define MAXPOSTPARAMS 50
#define MAXPOSTREDIRECTFILENAME (200)
/////////////////////////////////////////////////////////////////////////////
// typedefs
/////////////////////////////////////////////////////////////////////////////
typedef struct _tagPostParam {
// char* pchPageName;
struct {
char* pchParamName;
char* pchParamValue;
} stParams[MAXPOSTPARAMS];
int iNumParams;
char chFilename[MAXPOSTREDIRECTFILENAME];
} PostParam;
// multipart file upload post (per socket) structure
typedef struct {
char pchBoundaryValue[80];
OCTET oFileuploadStatus;
int iWriteLocation;
PostParam pp;
char *pchFilename;
} HttpMultipart;
typedef struct _tagSubstParam {
char* pchParamName;
char* pchParamValue; // returned
int iMaxValueBytes;
} SubstParam;
#define FLAG_REQUEST_GET 0x1
#define FLAG_REQUEST_POST 0x2
#define FLAG_CONN_CLOSE 0x10
#define FLAG_SUBST 0x20
#define FLAG_AUTHENTICATION 0x40
#define FLAG_MORE_CONTENT 0x80
#define FLAG_TO_FREE 0x100
#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_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];
} IP;
typedef struct {
IP ipAddr;
int iStartByte;
unsigned char *pucPath;
int ofReferer;
int ofHost;
int siHeaderSize;
unsigned char* pucPayload;
} HttpRequest;
typedef struct {
int iSentBytes;
int iContentLength;
HttpFileType fileType;
int iBufferSize; // the size of buffer pucData pointing to
} HttpResponse;
typedef struct {
char *name;
char *value;
} HttpVariables;
// Callback function protos
typedef int (*PFNPOSTCALLBACK)(PostParam*);
typedef int (*PFNSUBSTCALLBACK)(SubstParam*);
typedef int (*PFNFILEUPLOADCALLBACK)(char*, OCTET, OCTET*, DWORD);
typedef enum {
MW_INIT = 0,
MW_UNINIT,
} MW_EVENT;
typedef int (*MW_EVENT_HANDLER)(MW_EVENT msg, void* arg);
typedef struct {
time_t startTime;
int clientCount;
int clientCountMax;
int reqCount;
int reqGetCount;
int fileSentCount;
int fileSentBytes;
int varSubstCount;
int urlProcessCount;
int timeOutCount;
int authFailCount;
int reqPostCount;
int fileUploadCount;
} HttpStats;
#define HTTP_BUFFER_SIZE (4*1024 /*bytes*/)
// per connection/socket structure
typedef struct _HttpSocket{
struct _HttpSocket *next;
SOCKET socket;
int fd;
HttpRequest request;
HttpResponse response;
unsigned char *pucData;
int iDataLength;
unsigned long flags;
void* ptr;
time_t tmAcceptTime;
time_t tmExpirationTime;
int iRequestCount;
unsigned char buffer[HTTP_BUFFER_SIZE];
} HttpSocket;
typedef struct {
void* hp;
HttpSocket* hs;
char *pucRequest;
HttpVariables* pxVars;
int iVarCount;
char *pucHeader;
char *pucBuffer;
int iDataBytes;
int iContentBytes;
int iSentBytes;
HttpFileType fileType;
} UrlHandlerParam;
typedef int (*PFNURLCALLBACK)(UrlHandlerParam*);
typedef struct {
char* pchUrlPrefix;
PFNURLCALLBACK pfnUrlHandler;
MW_EVENT_HANDLER pfnEventHandler;
} UrlHandler;
#define FLAG_DIR_LISTING 1
#define FLAG_LOCAL_BIND 2
typedef struct _httpParam {
HttpSocket *phsSocketHead; /* head of the socket linked list */
int bKillWebserver;
int bWebserverRunning;
unsigned int flags;
SOCKET listenSocket;
int httpPort;
int maxReqPerConn; /* maximum requests on one connection */
int maxClients;
int socketRcvBufSize; /* socket receive buffer size in KB */
char *pchWebPath;
UrlHandler *pxUrlHandler; /* pointer to URL handler array */
// substitution callback
PFNSUBSTCALLBACK pfnSubst;
// post callbacks
PFNFILEUPLOADCALLBACK pfnFileUpload;
PFNPOSTCALLBACK pfnPost;
DWORD dwAuthenticatedNode;
time_t tmAuthExpireTime;
pthread_t tidHttpThread;
HttpStats stats;
} HttpParam;
typedef struct {
char* pchRootPath;
char* pchHttpPath;
char cFilePath[MAX_PATH];
char* pchExt;
int fTailSlash;
} 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
///////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////
// mwServerStart. Startup the webserver
///////////////////////////////////////////////////////////////////////
int mwServerStart(HttpParam* hp);
///////////////////////////////////////////////////////////////////////
// mwServerShutdown. Shutdown the webserver (closes connections and
// releases resources)
///////////////////////////////////////////////////////////////////////
int mwServerShutdown(HttpParam* hp);
///////////////////////////////////////////////////////////////////////
// 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(PFNPOSTCALLBACK);
///////////////////////////////////////////////////////////////////////
// mwFileUploadRegister. Specify the callback to be called whenever the
// server has the next data chunk available from a multipart file upload.
///////////////////////////////////////////////////////////////////////
PFNFILEUPLOADCALLBACK mwFileUploadRegister(PFNFILEUPLOADCALLBACK);
///////////////////////////////////////////////////////////////////////
// Default subst, post and file-upload callback processing
///////////////////////////////////////////////////////////////////////
int DefaultWebSubstCallback(SubstParam* sp);
int DefaultWebPostCallback(PostParam* pp);
int DefaultWebFileUploadCallback(char *pchFilename,
OCTET oFileuploadStatus,
OCTET *poData,
DWORD dwDataChunkSize);
int mwGetHttpDateTime(time_t tm, char *buf);
int mwGetLocalFileName(HttpFilePath* hfp);
char* mwGetVarValue(HttpVariables* vars, char *varname);
int mwGetVarValueInt(HttpVariables* vars, char *varname, int defval);
int mwParseQueryString(UrlHandlerParam* up);
#ifdef __cplusplus
}
#endif
#endif // _HTTPAPI_H
////////////////////////// END OF FILE ////////////////////////////////