1
0
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:
Will 2013-09-08 13:21:11 +09:00
parent 558a6b04ce
commit a2d7b361f9
3 changed files with 44 additions and 0 deletions

View File

@ -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");

View File

@ -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 *);

View File

@ -142,6 +142,7 @@ int main(int argc, char **argv) {
script_init(t->L, cfg.script, argc - optind, &argv[optind]);
if (i == 0) {
script_verify_request(t->L);
cfg.dynamic = !script_is_static(t->L);
if (script_want_response(t->L)) {
parser_settings.on_header_field = header_field;