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
|
#include <string.h>
#include <assert.h>
#include "http_decoder.h"
#include "http_decoder_inc.h"
char *safe_dup(const char *str, size_t len)
{
if (str == NULL || len == 0) {
return NULL;
}
char *dup = CALLOC(char, len + 1);
memcpy(dup, str, len);
return dup;
}
int strncasecmp_safe(const char *fix_s1, const char *dyn_s2, size_t fix_n1, size_t dyn_n2)
{
if (fix_s1 == NULL || dyn_s2 == NULL) {
return -1;
}
if(fix_n1 != dyn_n2){
return -1;
}
return strncasecmp(fix_s1, dyn_s2, fix_n1);
}
const char *http_message_type_to_string(enum http_message_type type)
{
const char *sname = "unknown_msg_type";
switch (type)
{
case HTTP_MESSAGE_REQ_LINE:
sname = "HTTP_MESSAGE_REQ_LINE";
break;
case HTTP_MESSAGE_REQ_HEADER:
sname = "HTTP_MESSAGE_REQ_HEADER";
break;
case HTTP_MESSAGE_REQ_HEADER_END:
sname = "HTTP_MESSAGE_REQ_HEADER_END";
break;
case HTTP_MESSAGE_REQ_BODY_START:
sname = "HTTP_MESSAGE_REQ_BODY_START";
break;
case HTTP_MESSAGE_REQ_BODY:
sname = "HTTP_MESSAGE_REQ_BODY";
break;
case HTTP_MESSAGE_REQ_BODY_END:
sname = "HTTP_MESSAGE_REQ_BODY_END";
break;
case HTTP_MESSAGE_RES_LINE:
sname = "HTTP_MESSAGE_RES_LINE";
break;
case HTTP_MESSAGE_RES_HEADER:
sname = "HTTP_MESSAGE_RES_HEADER";
break;
case HTTP_MESSAGE_RES_HEADER_END:
sname = "HTTP_MESSAGE_RES_HEADER_END";
break;
case HTTP_MESSAGE_RES_BODY_START:
sname = "HTTP_MESSAGE_RES_BODY_START";
break;
case HTTP_MESSAGE_RES_BODY:
sname = "HTTP_MESSAGE_RES_BODY";
break;
case HTTP_MESSAGE_RES_BODY_END:
sname = "HTTP_MESSAGE_RES_BODY_END";
break;
case HTTP_TRANSACTION_START:
sname = "HTTP_TRANSACTION_START";
break;
case HTTP_TRANSACTION_END:
sname = "HTTP_TRANSACTION_END";
break;
default:
break;
}
return sname;
}
int http_message_type_is_req(struct session *sess, enum http_message_type msg_type)
{
int is_req_msg = 0;
switch(msg_type){
case HTTP_MESSAGE_REQ_LINE:
case HTTP_MESSAGE_REQ_HEADER:
case HTTP_MESSAGE_REQ_HEADER_END:
case HTTP_MESSAGE_REQ_BODY_START:
case HTTP_MESSAGE_REQ_BODY:
case HTTP_MESSAGE_REQ_BODY_END:
is_req_msg = 1;
break;
case HTTP_MESSAGE_RES_LINE:
case HTTP_MESSAGE_RES_HEADER:
case HTTP_MESSAGE_RES_HEADER_END:
case HTTP_MESSAGE_RES_BODY_START:
case HTTP_MESSAGE_RES_BODY:
case HTTP_MESSAGE_RES_BODY_END:
is_req_msg = 0;
break;
case HTTP_TRANSACTION_START:
case HTTP_TRANSACTION_END:
{
int cur_dir = packet_get_direction(session_get0_current_packet(sess));
if(PACKET_DIRECTION_C2S == cur_dir){
is_req_msg = 1;
}else{
is_req_msg = 0;
}
}
break;
default:
assert(0);
fprintf(stderr, "unknow message type:%d\n", (int)msg_type);
break;
}
return is_req_msg;
}
int http_event_is_req(enum http_event event)
{
switch(event){
case HTTP_EVENT_REQ_INIT:
case HTTP_EVENT_REQ_LINE:
case HTTP_EVENT_REQ_HDR:
case HTTP_EVENT_REQ_HDR_END:
case HTTP_EVENT_REQ_BODY_BEGIN:
case HTTP_EVENT_REQ_BODY_DATA:
case HTTP_EVENT_REQ_BODY_END:
case HTTP_EVENT_REQ_END:
return 1;
break;
case HTTP_EVENT_RES_INIT:
case HTTP_EVENT_RES_LINE:
case HTTP_EVENT_RES_HDR:
case HTTP_EVENT_RES_HDR_END:
case HTTP_EVENT_RES_BODY_BEGIN:
case HTTP_EVENT_RES_BODY_DATA:
case HTTP_EVENT_RES_BODY_END:
case HTTP_EVENT_RES_END:
return 0;
break;
default:
assert(0);
fprintf(stderr, "unknow event type:%d\n", (int)event);
break;
}
return -1;
}
int stellar_session_mq_get_topic_id_reliable(struct stellar *st, const char *topic_name, session_msg_free_cb_func *msg_free_cb, void *msg_free_arg)
{
int topic_id = stellar_session_mq_get_topic_id(st, topic_name);
if(topic_id < 0){
topic_id = stellar_session_mq_create_topic(st, topic_name, msg_free_cb, msg_free_arg);
}
return topic_id;
}
static const unsigned char __g_httpd_hextable[] = {
0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 0, 0, 0, 0, 0, /* 0x30 - 0x3f */
0, 10, 11, 12, 13, 14, 15, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0x40 - 0x4f */
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0x50 - 0x5f */
0, 10, 11, 12, 13, 14, 15 /* 0x60 - 0x66 */
};
/* the input is a single hex digit */
#define onehex2dec(x) __g_httpd_hextable[x - '0']
#include <ctype.h>
// https://github.com/curl/curl/blob/2e930c333658725657b94a923d175c6622e0f41d/lib/urlapi.c
void httpd_url_decode(const char *string, size_t length, char *ostring, size_t *olen)
{
size_t alloc = length;
char *ns = ostring;
while (alloc)
{
unsigned char in = (unsigned char)*string;
if (('%' == in) && (alloc > 2) &&
isxdigit(string[1]) && isxdigit(string[2]))
{
/* this is two hexadecimal digits following a '%' */
in = (unsigned char)(onehex2dec(string[1]) << 4) | onehex2dec(string[2]);
string += 3;
alloc -= 3;
}
else
{
string++;
alloc--;
}
*ns++ = (char)in;
}
*ns = 0; /* terminate it */
if (olen)
/* store output size */
*olen = ns - ostring;
return;
}
int httpd_url_is_encoded(const char *url, size_t len)
{
for(size_t i = 0; i < len; i++){
if(url[i] == '%'){
return 1;
}
}
return 0;
}
|