-
Notifications
You must be signed in to change notification settings - Fork 0
/
x_http_common.h
155 lines (137 loc) · 4.51 KB
/
x_http_common.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
// x_http_common.h
#pragma once
#include "http_parser.h"
#include "socketsX.h"
#include "x_ubuf.h"
#include "yuarel.h"
#ifdef __cplusplus
extern "C" {
#endif
// ########################################### macros #############################################
#define httpYUAREL_MAX_PARTS 8
#define httpYUAREL_MAX_QUERY 8
#define httpHDR_VALUES(ct, acc, con, d) ((ct << 24) | (acc << 16) | (con << 8) | d)
#define httpNL "\r\n"
// ######################################### enumerations ##########################################
enum { // Header Fields
hfUNDEFINED = 0,
hfAccept,
hfAcceptCharset,
hfAcceptDatetime,
hfAcceptEncoding,
hfAcceptLanguage,
hfAcceptPatch,
hfAcceptRanges,
hfAuthorisation,
hfCacheControl,
hfConnection,
hfContentLength,
hfContentType,
hfDate,
hfHost,
hfLastModified,
hfPragma,
hfTransferEncoding,
hfNUMBER,
};
enum { // Content Types
ctUndefined = 0,
ctApplicationOctetStream,
ctApplicationJson,
ctApplicationXml,
ctTextPlain,
ctTextHtml,
ctApplicationXwwwFormUrlencoded,
#if 0 // below here not handled yet...
ctApplicationPostScript,
ctApplicationJavascript,
ctApplicationZip,
ctApplicationPdf,
ctAudioMpeg,
ctAudiovorbis,
ctMultipartFormData,
ctTextCss,
ctImagePng,
ctImageJpeg,
ctImageGif,
#endif
ctNUMBER,
};
enum { // Connection Types
coUNDEFINED = 0,
coKeepAlive,
coUpgrade,
coClose,
coNUMBER,
};
enum { // HTTP flags
httpFLAG_FROM = 1 << 0,
httpFLAG_AGENT = 1 << 1,
httpFLAG_HOST = 1 << 2,
httpFLAG_STATUS = 1 << 3,
httpFLAG_0 = 1 << 4,
httpFLAG_1 = 1 << 5,
httpFLAG_2 = 1 << 6,
httpFLAG_DEBUG = 1 << 7,
};
// ######################################### structures ############################################
typedef struct http_rr_t {
ubuf_t sUB; // both
netx_t sCtx; // both
// Sequence of parameters in pVarArg MUST be in same sequence as used by
// a) the pcQuery format string; and
// b) the pcBody format string
union {
pcc_t pcQuery; // client: 'format' GET/PUT/POST/DELETE/PATCH .....
pcc_t pcStatMes; // server: status message
};
const char * pcBody; // content/format
int (* hdlr) (struct http_rr_t *); // upload content handler
va_list VaList; // Client
void * pvArg; // Client
http_parser_settings sfCB; // Both
int onBodyRet; // result fromon_body handler
struct yuarel url; // Both
struct yuarel_param params[httpYUAREL_MAX_QUERY]; // Both
char * parts[httpYUAREL_MAX_PARTS]; // Both
u64_t hvContentLength; // Both
u32_t hvDate; // Both
u32_t hvLastModified; // Both
char * hvStatusMess; // Both
union {
struct __attribute__((packed)) { u8_t Spare, hvConnect, hvAccept, hvContentType; };
u32_t hvValues;
};
u16_t hvStatus; // Client (response to request)
u8_t HdrField; // Both
s8_t NumParts; // recognize -1 as error/none
s8_t NumQuery; // recognize -1 as error/none
union {
struct __attribute__((packed)) {
u8_t f_parts:1; // set to break URL up into parts
u8_t f_query:1; // set to break query up into parts
u8_t f_bodyCB:1; // set if pcBody contains a CB handler
u8_t f_host:1; // host info provided, or not ?
u8_t f_ac_rng:1; // accept ranges
};
u8_t f_allflags;
};
} http_rr_t;
typedef int (* hdlr_req_t) (struct http_rr_t *);
// ################################### Global variables ############################################
// ###################################### public functions #########################################
int xHttpCommonFindMatch(const char * const pcTable[], u32_t xSize, const char * pcMatch, size_t xLen);
int xHttpCommonMessageBeginHandler(http_parser * psParser);
int xHttpCommonUrlHandler(http_parser * psParser, const char * pBuf, size_t xLen);
int xHttpCommonStatusHandler(http_parser * psParser, const char * pBuf, size_t xLen);
int xHttpCommonHeaderFieldHandler(http_parser * psParser, const char * pBuf, size_t xLen);
int xHttpCommonHeaderValueHandler(http_parser * psParser, const char * pBuf, size_t xLen);
int xHttpCommonHeadersCompleteHandler(http_parser * psParser);
int xHttpCommonChunkHeaderHandler(http_parser * psParser);
int xHttpCommonChunkCompleteHandler(http_parser * psParser);
int xHttpCommonMessageBodyHandler(http_parser * psParser, const char * pBuf, size_t xLen);
int xHttpCommonMessageCompleteHandler(http_parser * psParser);
size_t xHttpCommonDoParsing(http_parser * psParser);
#ifdef __cplusplus
}
#endif