mirror of
https://github.com/wg/wrk
synced 2025-01-23 20:23:03 +08:00
check if generated request is valid
This commit is contained in:
parent
558a6b04ce
commit
a2d7b361f9
42
src/script.c
42
src/script.c
@ -3,6 +3,7 @@
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include "script.h"
|
||||
#include "http_parser.h"
|
||||
|
||||
typedef struct {
|
||||
char *name;
|
||||
@ -177,6 +178,47 @@ void script_done(lua_State *L, stats *latency, stats *requests) {
|
||||
lua_pop(L, 1);
|
||||
}
|
||||
|
||||
static int verify_request(http_parser *parser) {
|
||||
size_t *count = parser->data;
|
||||
(*count)++;
|
||||
return 0;
|
||||
}
|
||||
|
||||
size_t script_verify_request(lua_State *L) {
|
||||
http_parser_settings settings = {
|
||||
.on_message_complete = verify_request
|
||||
};
|
||||
http_parser parser;
|
||||
char *request;
|
||||
size_t len, count = 0;
|
||||
|
||||
script_request(L, &request, &len);
|
||||
http_parser_init(&parser, HTTP_REQUEST);
|
||||
parser.data = &count;
|
||||
|
||||
size_t parsed = http_parser_execute(&parser, &settings, request, len);
|
||||
|
||||
if (parsed != len || count == 0) {
|
||||
enum http_errno err = HTTP_PARSER_ERRNO(&parser);
|
||||
const char *desc = http_errno_description(err);
|
||||
const char *msg = err != HPE_OK ? desc : "incomplete request";
|
||||
int line = 1, column = 1;
|
||||
|
||||
for (char *c = request; c < request + parsed; c++) {
|
||||
column++;
|
||||
if (*c == '\n') {
|
||||
column = 1;
|
||||
line++;
|
||||
}
|
||||
}
|
||||
|
||||
fprintf(stderr, "%s at %d:%d\n", msg, line, column);
|
||||
exit(1);
|
||||
}
|
||||
|
||||
return count;
|
||||
}
|
||||
|
||||
static stats *checkstats(lua_State *L) {
|
||||
stats **s = luaL_checkudata(L, 1, "wrk.stats");
|
||||
luaL_argcheck(L, s != NULL, 1, "`stats' expected");
|
||||
|
@ -15,6 +15,7 @@ typedef struct {
|
||||
|
||||
lua_State *script_create(char *, char *, char *, char *);
|
||||
void script_headers(lua_State *, char **);
|
||||
size_t script_verify_request(lua_State *L);
|
||||
|
||||
void script_init(lua_State *, char *, int, char **);
|
||||
void script_done(lua_State *, stats *, stats *);
|
||||
|
Loading…
Reference in New Issue
Block a user