59 static int HttpGetRequestHost(
lua_State *luastate)
62 return LuaCallbackError(luastate,
"error: protocol not http");
64 htp_tx_t *tx = LuaStateGetTX(luastate);
66 return LuaCallbackError(luastate,
"internal error: no tx");
68 if (tx->request_hostname == NULL)
69 return LuaCallbackError(luastate,
"no request hostname");
71 return LuaPushStringBuffer(luastate,
72 bstr_ptr(tx->request_hostname), bstr_len(tx->request_hostname));
75 static int HttpGetRequestUriRaw(
lua_State *luastate)
78 return LuaCallbackError(luastate,
"error: protocol not http");
80 htp_tx_t *tx = LuaStateGetTX(luastate);
82 return LuaCallbackError(luastate,
"internal error: no tx");
84 if (tx->request_uri == NULL)
85 return LuaCallbackError(luastate,
"no request uri");
87 return LuaPushStringBuffer(luastate,
88 bstr_ptr(tx->request_uri), bstr_len(tx->request_uri));
91 static int HttpGetRequestUriNormalized(
lua_State *luastate)
94 return LuaCallbackError(luastate,
"error: protocol not http");
96 htp_tx_t *tx = LuaStateGetTX(luastate);
98 return LuaCallbackError(luastate,
"internal error: no tx");
102 return LuaCallbackError(luastate,
"no htud in tx");
107 return LuaCallbackError(luastate,
"no normalized uri");
109 return LuaPushStringBuffer(luastate,
114 static int HttpGetRequestLine(
lua_State *luastate)
117 return LuaCallbackError(luastate,
"error: protocol not http");
119 htp_tx_t *tx = LuaStateGetTX(luastate);
121 return LuaCallbackError(luastate,
"internal error: no tx");
123 if (tx->request_line == NULL)
124 return LuaCallbackError(luastate,
"no request_line");
126 return LuaPushStringBuffer(luastate,
127 bstr_ptr(tx->request_line), bstr_len(tx->request_line));
130 static int HttpGetResponseLine(
lua_State *luastate)
133 return LuaCallbackError(luastate,
"error: protocol not http");
135 htp_tx_t *tx = LuaStateGetTX(luastate);
137 return LuaCallbackError(luastate,
"internal error: no tx");
139 if (tx->response_line == NULL)
140 return LuaCallbackError(luastate,
"no response_line");
142 return LuaPushStringBuffer(luastate,
143 bstr_ptr(tx->response_line), bstr_len(tx->response_line));
146 static int HttpGetHeader(
lua_State *luastate,
int dir)
149 return LuaCallbackError(luastate,
"error: protocol not http");
151 htp_tx_t *tx = LuaStateGetTX(luastate);
153 return LuaCallbackError(luastate,
"internal error: no tx");
155 const char *name = LuaGetStringArgument(luastate, 1);
157 return LuaCallbackError(luastate,
"1st argument missing, empty or wrong type");
159 htp_table_t *headers = tx->request_headers;
161 headers = tx->response_headers;
163 return LuaCallbackError(luastate,
"tx has no headers");
165 htp_header_t *h = (htp_header_t *)htp_table_get_c(headers, name);
166 if (h == NULL || bstr_len(h->value) == 0)
167 return LuaCallbackError(luastate,
"header not found");
169 return LuaPushStringBuffer(luastate,
170 bstr_ptr(h->value), bstr_len(h->value));
173 static int HttpGetRequestHeader(
lua_State *luastate)
175 return HttpGetHeader(luastate, 0 );
178 static int HttpGetResponseHeader(
lua_State *luastate)
180 return HttpGetHeader(luastate, 1 );
183 static int HttpGetRawHeaders(
lua_State *luastate,
int dir)
186 return LuaCallbackError(luastate,
"error: protocol not http");
188 htp_tx_t *tx = LuaStateGetTX(luastate);
190 return LuaCallbackError(luastate,
"internal error: no tx");
194 return LuaCallbackError(luastate,
"no htud in tx");
203 if (raw == NULL || raw_len == 0)
204 return LuaCallbackError(luastate,
"no raw headers");
206 return LuaPushStringBuffer(luastate, raw, raw_len);
209 static int HttpGetRawRequestHeaders(
lua_State *luastate)
211 return HttpGetRawHeaders(luastate, 0);
214 static int HttpGetRawResponseHeaders(
lua_State *luastate)
216 return HttpGetRawHeaders(luastate, 1);
220 static int HttpGetHeaders(
lua_State *luastate,
int dir)
223 return LuaCallbackError(luastate,
"error: protocol not http");
225 htp_tx_t *tx = LuaStateGetTX(luastate);
227 return LuaCallbackError(luastate,
"internal error: no tx");
229 htp_table_t *table = tx->request_headers;
231 table = tx->response_headers;
232 if (tx->request_headers == NULL)
233 return LuaCallbackError(luastate,
"no headers");
235 lua_newtable(luastate);
236 htp_header_t *h = NULL;
238 size_t no_of_headers = htp_table_size(table);
239 for (; i < no_of_headers; i++) {
240 h = htp_table_get_index(table, i, NULL);
241 LuaPushStringBuffer(luastate, bstr_ptr(h->name), bstr_len(h->name));
242 LuaPushStringBuffer(luastate, bstr_ptr(h->value), bstr_len(h->value));
243 lua_settable(luastate, -3);
249 static int HttpGetRequestHeaders(
lua_State *luastate)
251 return HttpGetHeaders(luastate, 0);
255 static int HttpGetResponseHeaders(
lua_State *luastate)
257 return HttpGetHeaders(luastate, 1);
260 static int HttpGetBody(
lua_State *luastate,
int dir)
265 return LuaCallbackError(luastate,
"error: protocol not http");
267 htp_tx_t *tx = LuaStateGetTX(luastate);
269 return LuaCallbackError(luastate,
"internal error: no tx");
273 return LuaCallbackError(luastate,
"no htud in tx");
280 if (body->
first == NULL)
281 return LuaCallbackError(luastate,
"no body");
285 lua_newtable(luastate);
286 while (chunk != NULL) {
287 lua_pushinteger(luastate, index);
289 const uint8_t *data = NULL;
290 uint32_t data_len = 0;
292 LuaPushStringBuffer(luastate, data, data_len);
294 lua_settable(luastate, -3);
301 lua_pushinteger(luastate, body->
first->
sbseg.stream_offset);
302 lua_pushinteger(luastate, body->
last->
sbseg.stream_offset + body->
last->
sbseg.segment_len);
309 static int HttpGetRequestBody(
lua_State *luastate)
311 return HttpGetBody(luastate, 0);
314 static int HttpGetResponseBody(
lua_State *luastate)
316 return HttpGetBody(luastate, 1);
320 int LuaRegisterHttpFunctions(
lua_State *luastate)
323 lua_pushcfunction(luastate, HttpGetRequestHeader);
324 lua_setglobal(luastate,
"HttpGetRequestHeader");
325 lua_pushcfunction(luastate, HttpGetResponseHeader);
326 lua_setglobal(luastate,
"HttpGetResponseHeader");
327 lua_pushcfunction(luastate, HttpGetRequestLine);
328 lua_setglobal(luastate,
"HttpGetRequestLine");
329 lua_pushcfunction(luastate, HttpGetResponseLine);
330 lua_setglobal(luastate,
"HttpGetResponseLine");
331 lua_pushcfunction(luastate, HttpGetRawRequestHeaders);
332 lua_setglobal(luastate,
"HttpGetRawRequestHeaders");
333 lua_pushcfunction(luastate, HttpGetRawResponseHeaders);
334 lua_setglobal(luastate,
"HttpGetRawResponseHeaders");
335 lua_pushcfunction(luastate, HttpGetRequestUriRaw);
336 lua_setglobal(luastate,
"HttpGetRequestUriRaw");
337 lua_pushcfunction(luastate, HttpGetRequestUriNormalized);
338 lua_setglobal(luastate,
"HttpGetRequestUriNormalized");
339 lua_pushcfunction(luastate, HttpGetRequestHeaders);
340 lua_setglobal(luastate,
"HttpGetRequestHeaders");
341 lua_pushcfunction(luastate, HttpGetResponseHeaders);
342 lua_setglobal(luastate,
"HttpGetResponseHeaders");
343 lua_pushcfunction(luastate, HttpGetRequestHost);
344 lua_setglobal(luastate,
"HttpGetRequestHost");
346 lua_pushcfunction(luastate, HttpGetRequestBody);
347 lua_setglobal(luastate,
"HttpGetRequestBody");
348 lua_pushcfunction(luastate, HttpGetResponseBody);
349 lua_setglobal(luastate,
"HttpGetResponseBody");