summaryrefslogtreecommitdiff
path: root/common/include/tfe_http.h
blob: cffed8ff463d3b4ff98800d2adf1b43cda8dc04a (plain)
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
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
#pragma once
#include <stdint.h>
#include <stddef.h>
#include <stdlib.h>
#include <assert.h>
#include <string.h>

#include <tfe_utils.h>
#include <tfe_future.h>
#include <tfe_stream.h>

/* Copy from http_parser.h */
#define __HTTP_METHOD_MAP(XX)       \
  XX(0,  DELETE,      DELETE)       \
  XX(1,  GET,         GET)          \
  XX(2,  HEAD,        HEAD)         \
  XX(3,  POST,        POST)         \
  XX(4,  PUT,         PUT)          \
  /* pathological */                \
  XX(5,  CONNECT,     CONNECT)      \
  XX(6,  OPTIONS,     OPTIONS)      \
  XX(7,  TRACE,       TRACE)        \
  /* WebDAV */                      \
  XX(8,  COPY,        COPY)         \
  XX(9,  LOCK,        LOCK)         \
  XX(10, MKCOL,       MKCOL)        \
  XX(11, MOVE,        MOVE)         \
  XX(12, PROPFIND,    PROPFIND)     \
  XX(13, PROPPATCH,   PROPPATCH)    \
  XX(14, SEARCH,      SEARCH)       \
  XX(15, UNLOCK,      UNLOCK)       \
  XX(16, BIND,        BIND)         \
  XX(17, REBIND,      REBIND)       \
  XX(18, UNBIND,      UNBIND)       \
  XX(19, ACL,         ACL)          \
  /* subversion */                  \
  XX(20, REPORT,      REPORT)       \
  XX(21, MKACTIVITY,  MKACTIVITY)   \
  XX(22, CHECKOUT,    CHECKOUT)     \
  XX(23, MERGE,       MERGE)        \
  /* upnp */                        \
  XX(24, MSEARCH,     M-SEARCH)     \
  XX(25, NOTIFY,      NOTIFY)       \
  XX(26, SUBSCRIBE,   SUBSCRIBE)    \
  XX(27, UNSUBSCRIBE, UNSUBSCRIBE)  \
  /* RFC-5789 */                    \
  XX(28, PATCH,       PATCH)        \
  XX(29, PURGE,       PURGE)        \
  /* CalDAV */                      \
  XX(30, MKCALENDAR,  MKCALENDAR)   \
  /* RFC-2068, section 19.6.1.2 */  \
  XX(31, LINK,        LINK)         \
  XX(32, UNLINK,      UNLINK)       \
  /* icecast */                     \
  XX(33, SOURCE,      SOURCE)       \

/* Status Codes */
#define __HTTP_STATUS_MAP(XX)                                               \
  XX(100, CONTINUE,                        Continue)                        \
  XX(101, SWITCHING_PROTOCOLS,             Switching Protocols)             \
  XX(102, PROCESSING,                      Processing)                      \
  XX(200, OK,                              OK)                              \
  XX(201, CREATED,                         Created)                         \
  XX(202, ACCEPTED,                        Accepted)                        \
  XX(203, NON_AUTHORITATIVE_INFORMATION,   Non-Authoritative Information)   \
  XX(204, NO_CONTENT,                      No Content)                      \
  XX(205, RESET_CONTENT,                   Reset Content)                   \
  XX(206, PARTIAL_CONTENT,                 Partial Content)                 \
  XX(207, MULTI_STATUS,                    Multi-Status)                    \
  XX(208, ALREADY_REPORTED,                Already Reported)                \
  XX(226, IM_USED,                         IM Used)                         \
  XX(300, MULTIPLE_CHOICES,                Multiple Choices)                \
  XX(301, MOVED_PERMANENTLY,               Moved Permanently)               \
  XX(302, FOUND,                           Found)                           \
  XX(303, SEE_OTHER,                       See Other)                       \
  XX(304, NOT_MODIFIED,                    Not Modified)                    \
  XX(305, USE_PROXY,                       Use Proxy)                       \
  XX(307, TEMPORARY_REDIRECT,              Temporary Redirect)              \
  XX(308, PERMANENT_REDIRECT,              Permanent Redirect)              \
  XX(400, BAD_REQUEST,                     Bad Request)                     \
  XX(401, UNAUTHORIZED,                    Unauthorized)                    \
  XX(402, PAYMENT_REQUIRED,                Payment Required)                \
  XX(403, FORBIDDEN,                       Forbidden)                       \
  XX(404, NOT_FOUND,                       Not Found)                       \
  XX(405, METHOD_NOT_ALLOWED,              Method Not Allowed)              \
  XX(406, NOT_ACCEPTABLE,                  Not Acceptable)                  \
  XX(407, PROXY_AUTHENTICATION_REQUIRED,   Proxy Authentication Required)   \
  XX(408, REQUEST_TIMEOUT,                 Request Timeout)                 \
  XX(409, CONFLICT,                        Conflict)                        \
  XX(410, GONE,                            Gone)                            \
  XX(411, LENGTH_REQUIRED,                 Length Required)                 \
  XX(412, PRECONDITION_FAILED,             Precondition Failed)             \
  XX(413, PAYLOAD_TOO_LARGE,               Payload Too Large)               \
  XX(414, URI_TOO_LONG,                    URI Too Long)                    \
  XX(415, UNSUPPORTED_MEDIA_TYPE,          Unsupported Media Type)          \
  XX(416, RANGE_NOT_SATISFIABLE,           Range Not Satisfiable)           \
  XX(417, EXPECTATION_FAILED,              Expectation Failed)              \
  XX(421, MISDIRECTED_REQUEST,             Misdirected Request)             \
  XX(422, UNPROCESSABLE_ENTITY,            Unprocessable Entity)            \
  XX(423, LOCKED,                          Locked)                          \
  XX(424, FAILED_DEPENDENCY,               Failed Dependency)               \
  XX(426, UPGRADE_REQUIRED,                Upgrade Required)                \
  XX(428, PRECONDITION_REQUIRED,           Precondition Required)           \
  XX(429, TOO_MANY_REQUESTS,               Too Many Requests)               \
  XX(431, REQUEST_HEADER_FIELDS_TOO_LARGE, Request Header Fields Too Large) \
  XX(451, UNAVAILABLE_FOR_LEGAL_REASONS,   Unavailable For Legal Reasons)   \
  XX(500, INTERNAL_SERVER_ERROR,           Internal Server Error)           \
  XX(501, NOT_IMPLEMENTED,                 Not Implemented)                 \
  XX(502, BAD_GATEWAY,                     Bad Gateway)                     \
  XX(503, SERVICE_UNAVAILABLE,             Service Unavailable)             \
  XX(504, GATEWAY_TIMEOUT,                 Gateway Timeout)                 \
  XX(505, HTTP_VERSION_NOT_SUPPORTED,      HTTP Version Not Supported)      \
  XX(506, VARIANT_ALSO_NEGOTIATES,         Variant Also Negotiates)         \
  XX(507, INSUFFICIENT_STORAGE,            Insufficient Storage)            \
  XX(508, LOOP_DETECTED,                   Loop Detected)                   \
  XX(510, NOT_EXTENDED,                    Not Extended)                    \
  XX(511, NETWORK_AUTHENTICATION_REQUIRED, Network Authentication Required) \

enum tfe_http_std_method
{
#define XX(num, name, string) TFE_HTTP_METHOD_##name = num,
	__HTTP_METHOD_MAP(XX)
#undef XX
};

enum tfe_http_std_status
{
#define XX(num, name, string) TFE_HTTP_STATUS_##name = num,
	__HTTP_STATUS_MAP(XX)
#undef XX
};

enum tfe_http_std_field
{
	TFE_HTTP_UNKNOWN_FIELD = 0,
	TFE_HTTP_HOST,
	TFE_HTTP_REFERER,
	TFE_HTTP_USER_AGENT,
	TFE_HTTP_COOKIE,
	TFE_HTTP_SET_COOKIE,
	TFE_HTTP_PROXY_AUTHORIZATION,
	TFE_HTTP_AUTHORIZATION,
	TFE_HTTP_LOCATION,
	TFE_HTTP_SERVER,
	TFE_HTTP_ETAG,
	TFE_HTTP_DATE,
	TFE_HTTP_TRAILER,
	TFE_HTTP_TRANSFER_ENCODING,
	TFE_HTTP_VIA,
	TFE_HTTP_PRAGMA,
	TFE_HTTP_CONNECTION,
	TFE_HTTP_CONT_ENCODING,
	TFE_HTTP_CONT_LANGUAGE,
	TFE_HTTP_CONT_LOCATION,
	TFE_HTTP_CONT_RANGE,
	TFE_HTTP_CONT_LENGTH,
	TFE_HTTP_CONT_TYPE,
	TFE_HTTP_CONT_DISPOSITION,
	TFE_HTTP_EXPIRES,
	TFE_HTTP_ACCEPT_ENCODING,
	TFE_HTTP_CACHE_CONTROL,
	TFE_HTTP_IF_MATCH,
	TFE_HTTP_IF_NONE_MATCH,
	TFE_HTTP_IF_MODIFIED_SINCE,
	TFE_HTTP_IF_UNMODIFIED_SINCE,
	TFE_HTTP_LAST_MODIFIED
};

enum http_ev_bit_number
{
	REQ_HDR_BITNUM = 0,
	REQ_BODY_BEGIN_BITNUM,
	REQ_BODY_CONT_BITNUM,
	REQ_BODY_END_BITNUM,
	REQ_END_BITNUM,
	RESP_HDR_BITNUM,
	RESP_BODY_BEGIN_BITNUM,
	RESP_BODY_CONT_BITNUM,
	RESP_BODY_END_BITNUM,
	RESP_END_BITNUM
};

enum tfe_http_event
{
	EV_HTTP_REQ_HDR = 1ULL << REQ_HDR_BITNUM,
	EV_HTTP_REQ_BODY_BEGIN = 1ULL << REQ_BODY_BEGIN_BITNUM,
	EV_HTTP_REQ_BODY_CONT = 1ULL << REQ_BODY_CONT_BITNUM,
	EV_HTTP_REQ_BODY_END = 1ULL << REQ_BODY_END_BITNUM,
	EV_HTTP_REQ_END = 1ULL << REQ_END_BITNUM,
	EV_HTTP_RESP_HDR = 1ULL << RESP_HDR_BITNUM,
	EV_HTTP_RESP_BODY_BEGIN = 1ULL << RESP_BODY_BEGIN_BITNUM,
	EV_HTTP_RESP_BODY_CONT = 1ULL << RESP_BODY_CONT_BITNUM,
	EV_HTTP_RESP_BODY_END = 1ULL << RESP_BODY_END_BITNUM,
	EV_HTTP_RESP_END = 1ULL << RESP_END_BITNUM,
};

struct tfe_http_req_spec
{
	enum tfe_http_std_method method;
	const char * uri;
	const char * host;
	const char * url; //uri+host
};

struct tfe_http_resp_spec
{
	int resp_code;
	const char * content_encoding;
	const char * content_type;
	const char * content_length;
};

enum tfe_http_direction
{
	TFE_HTTP_REQUEST = 1,
	TFE_HTTP_RESPONSE = 2
};

struct tfe_http_half_ops
{
	const char * (* ops_http_field_read)(const struct tfe_http_half *, const struct http_field_name *);
	int (* ops_http_field_write)(struct tfe_http_half *, const struct http_field_name *, const char *);
	struct tfe_http_half * (* ops_http_allow_write)(const struct tfe_http_half *);
	const char * (* ops_http_field_iterate)(const struct tfe_http_half *, void **, struct http_field_name *);
	int (* ops_append_body)(struct tfe_http_half *, char *, size_t, int);
	int (* ops_body_begin)(struct tfe_http_half *, int by_stream);
	int (* ops_body_data)(struct tfe_http_half *, const unsigned char * data, size_t sz_data);
	int (* ops_body_end)(struct tfe_http_half *);
	void (* ops_free)(struct tfe_http_half * half);
};

struct tfe_http_session_ops
{
	struct tfe_http_session * (* ops_allow_write)(const struct tfe_http_session * session);
	void (* ops_detach)(const struct tfe_http_session * session);
	void (* ops_drop)(struct tfe_http_session * session);
	void (* ops_suspend)(struct tfe_http_session * session);
	void (* ops_resume)(struct tfe_http_session * session);
	void (* ops_kill)(struct tfe_http_session * session);

	void (* ops_request_set)(struct tfe_http_session * session, struct tfe_http_half * req);
	void (* ops_response_set)(struct tfe_http_session * session, struct tfe_http_half * resp);

	struct tfe_http_half * (* ops_request_create)(struct tfe_http_session * session,
		enum tfe_http_std_method method, const char * uri);
	struct tfe_http_half * (* ops_response_create)(struct tfe_http_session * session, int resp_code);
};

struct tfe_http_half
{
	enum tfe_http_direction direction;
	const struct tfe_http_half_ops * ops;
	short major_version;
	short minor_version;

	union
	{
		struct tfe_http_req_spec req_spec;
		struct tfe_http_resp_spec resp_spec;
	};
};

struct tfe_http_session
{
	unsigned int session_id;
	short major_version;
	short minor_version;

	struct tfe_http_session_ops * ops;
	struct tfe_http_half * req;
	struct tfe_http_half * resp;
};

struct http_field_name
{
	/* Standard Field */
	enum tfe_http_std_field field_id;
	/* Non-NULL when field_id is HTTP_UNKNOWN_FIELD */
	const char * field_name;
};

typedef void (http_session_begin_cb)(const struct tfe_stream * stream,
	const struct tfe_http_session * session, unsigned int thread_id, void ** pme);

typedef void (http_session_data_cb)(const struct tfe_stream * stream,
	const struct tfe_http_session * session, enum tfe_http_event event, const unsigned char * data,
	size_t datalen, unsigned int thread_id, void ** pme);

typedef void (http_session_end_cb)(const struct tfe_stream * stream,
	const struct tfe_http_session * session, unsigned int thread_id, void ** pme);

/* Tools functions for standard HTTP header field name */
struct http_field_name * http_field_name_duplicate(const struct http_field_name * orig);
int http_field_name_compare(const struct http_field_name * lvalue, const struct http_field_name * rvalue);
const char * http_field_name_to_string(const struct http_field_name * field);
void http_field_name_destory(struct http_field_name *);

/* Tools functions for standard HTTP method */
const char * http_std_method_to_string(enum tfe_http_std_method method);
enum tfe_http_std_method http_std_method_construct(const char * str_method);

/* Tools functions for standard HTTP status */
const char * http_std_status_to_string(enum tfe_http_std_status status);
enum tfe_http_std_status http_std_status_construct(const char * str_status);

static inline const char * tfe_http_field_read(const struct tfe_http_half * half,
	const struct http_field_name * name)
{
	return half->ops->ops_http_field_read(half, name);
}

static inline const char * tfe_http_std_field_read(const struct tfe_http_half * half, enum tfe_http_std_field field_id)
{
	struct http_field_name tmp_name;
	tmp_name.field_id=field_id;
	tmp_name.field_name=NULL;
	return tfe_http_field_read(half, &tmp_name);
}
static inline const char * tfe_http_nonstd_field_read(const struct tfe_http_half * half, const char* field)
{
	struct http_field_name tmp_name;
	tmp_name.field_id=TFE_HTTP_UNKNOWN_FIELD;
	tmp_name.field_name=field;
	return tfe_http_field_read(half, &tmp_name);
}

static inline int tfe_http_field_write(struct tfe_http_half * half,
	const struct http_field_name * name, const char * value)
{
	return half->ops->ops_http_field_write(half, name, value);
}

static inline int tfe_http_std_field_write(struct tfe_http_half * half,
	enum tfe_http_std_field field_id, const char * value)
{
	struct http_field_name tmp_name;
	tmp_name.field_id=field_id;
	tmp_name.field_name=NULL;
	return tfe_http_field_write(half, &tmp_name, value);
}
static inline int tfe_http_nonstd_field_write(struct tfe_http_half * half,
	const char* field, const char * value)
{
	struct http_field_name tmp_name;
	tmp_name.field_id=TFE_HTTP_UNKNOWN_FIELD;
	tmp_name.field_name=field;
	return tfe_http_field_write(half, &tmp_name, value);
}

static inline struct tfe_http_half * tfe_http_allow_write(const struct tfe_http_half * half)
{
	return half->ops->ops_http_allow_write(half);
}

static inline const char * tfe_http_field_iterate(const struct tfe_http_half * half, void ** iterator,
	struct http_field_name * name)
{
	return half->ops->ops_http_field_iterate(half, iterator, name);
}

static inline int tfe_http_half_append_body(struct tfe_http_half * half, char * buff, size_t size, int flag)
{
	return half->ops->ops_append_body(half, buff, size, flag);
}

static inline void tfe_http_half_free(struct tfe_http_half * half)
{
	return half->ops->ops_free(half);
}

static inline int tfe_http_half_write_body_begin(struct tfe_http_half * half, int by_stream)
{
	return half->ops->ops_body_begin(half, by_stream);
}

static inline int tfe_http_half_write_body_data(struct tfe_http_half * half, const unsigned char * data, size_t sz_data)
{
	return half->ops->ops_body_data(half, data, sz_data);
}

static inline int tfe_http_half_write_body_end(struct tfe_http_half * half)
{
	return half->ops->ops_body_end(half);
}

static inline struct tfe_http_session * tfe_http_session_allow_write(const struct tfe_http_session * session)
{
	return session->ops->ops_allow_write(session);
}

static inline void tfe_http_session_detach(const struct tfe_http_session * session)
{
	return session->ops->ops_detach(session);
}

static inline void tfe_http_session_drop(struct tfe_http_session * session)
{
	return session->ops->ops_drop(session);
}

static inline void tfe_http_session_kill(struct tfe_http_session * session)
{
	return session->ops->ops_kill(session);
}

static inline void tfe_http_session_suspend(struct tfe_http_session * session)
{
	return session->ops->ops_suspend(session);
}

static inline void tfe_http_session_resume(struct tfe_http_session * session)
{
	return session->ops->ops_resume(session);
}

static inline void tfe_http_session_request_set(struct tfe_http_session * session, struct tfe_http_half * req)
{
	return session->ops->ops_request_set(session, req);
}

static inline void tfe_http_session_response_set(struct tfe_http_session * session, struct tfe_http_half * resp)
{
	return session->ops->ops_response_set(session, resp);
}

static inline struct tfe_http_half * tfe_http_session_request_create(struct tfe_http_session * session,
	enum tfe_http_std_method method, const char * uri)
{
	return session->ops->ops_request_create(session, method, uri);
}

static inline struct tfe_http_half * tfe_http_session_response_create(struct tfe_http_session * session, int resp_code)
{
	return session->ops->ops_response_create(session, resp_code);
}
static inline int tfe_http_in_request(enum tfe_http_event events)
{
	if ((events & EV_HTTP_REQ_HDR) | (events & EV_HTTP_REQ_BODY_BEGIN) | (events & EV_HTTP_REQ_BODY_END)
		| (events & EV_HTTP_REQ_BODY_CONT)| (events & EV_HTTP_REQ_END))
	{
		return 1;
	}
	else
	{
		return 0;
	}
}
static inline int tfe_http_in_response(enum tfe_http_event events)
{
	return !(tfe_http_in_request(events));
}

static inline struct tfe_http_half* tfe_http_session_request_duplicate(struct tfe_http_session * session)
{
	struct http_field_name in_header_field{};
	const char * in_header_value = NULL;
	void * iterator = NULL;

	struct tfe_http_half* dup_req=tfe_http_session_request_create(session, session->req->req_spec.method, session->req->req_spec.uri);
	while (true)
	{
		if ((in_header_value = tfe_http_field_iterate(session->req, &iterator, &in_header_field)) == NULL)
		{
			break;
		}
		tfe_http_field_write(dup_req, &in_header_field,in_header_value);
	}
	return dup_req;
}

//@flag EV_HTTP_RESP_BODY_END, EV_HTTP_RESP_BODY_FULL,
//suspend stream on EV_HTTP_REQ_BODY_BEGIN, resume when EV_HTTP_REQ_BODY_END.


/*
handler_
{
    tfe_http_response_frag_create;
    tfe_http_session_set_half;
}

rpc_cb_
{
    tfe_http_set_field
    tfe_http_append_body

    tfe_http_write();
}


rpc_finish_cb_
{
    tfe_http_write_finish();
};
*/

/* TODO: transfer these declaration to ht_frame.h */
struct http_frame_plugin_status
{
	void * pme;
	unsigned int detached;
	unsigned int is_preempted;
	unsigned int preempt_by;
};

struct http_frame_session_ctx * http_frame_alloc();

int http_frame_raise_session_begin(struct http_frame_session_ctx * ht_frame, const tfe_stream * stream,
	struct tfe_http_session * ht_session, unsigned int thread_id);

void http_frame_raise_session_end(struct http_frame_session_ctx * ht_frame, const tfe_stream * stream,
	struct tfe_http_session * ht_session, unsigned int thread_id);

void http_frame_raise_event(struct http_frame_session_ctx * ht_frame,
	const struct tfe_stream * stream, struct tfe_http_session * ht_session, enum tfe_http_event event,
	const unsigned char * data, size_t datalen, unsigned int thread_id);

void http_frame_currect_plugin_detach(struct http_frame_session_ctx * ht_frame);
int http_frame_currect_plugin_preempt(struct http_frame_session_ctx * ht_frame);