static int lhp_error(lua_State* L) { lhttp_parser* lparser = check_parser(L, 1); enum http_errno http_errno = lparser->parser.http_errno; lua_pushnumber(L, http_errno); lua_pushstring(L, http_errno_name(http_errno)); lua_pushstring(L, http_errno_description(http_errno)); return 3; }
static int lhp_execute(lua_State* L) { lhttp_parser* lparser = check_parser(L, 1); http_parser* parser = &(lparser->parser); size_t len; size_t result; const char* str = luaL_checklstring(L, 2, &len); static const http_parser_settings settings = { .on_message_begin = lhp_message_begin_cb, .on_url = lhp_url_cb, .on_header_field = lhp_header_field_cb, .on_header_value = lhp_header_value_cb, .on_headers_complete = lhp_headers_complete_cb, .on_body = lhp_body_cb, .on_message_complete = lhp_message_complete_cb }; /* truncate stack to (userdata, string) */ lua_settop(L, 2); lua_getfenv(L, 1); assert(lua_istable(L, -1)); assert(lua_gettop(L) == ST_FENV_IDX); lua_rawgeti(L, ST_FENV_IDX, FENV_BUFFER_IDX); assert(lua_istable(L, -1)); assert(lua_gettop(L) == ST_BUFFER_IDX); assert(lua_gettop(L) == ST_LEN); lua_pushnil(L); /* Stack: (userdata, string, fenv, buffer, url, nil) */ parser->data = L; result = http_parser_execute(parser, &settings, str, len); parser->data = NULL; /* replace nil place-holder with 'result' code. */ lua_pushnumber(L, result); lua_replace(L, ST_LEN+1); /* Transform the stack into a table: */ len = lua_gettop(L) - ST_LEN; return len; }
static int lhp_reset(lua_State* L) { lhttp_parser* lparser = check_parser(L, 1); http_parser* parser = &(lparser->parser); /* truncate stack to (userdata) */ lua_settop(L, 1); /* re-initialize http-parser. */ http_parser_init(parser, parser->type); /* clear buffer */ lua_getfenv(L, 1); lua_rawgeti(L, 2, FENV_BUFFER_IDX); lhp_table_clear(L, 3, 1, lparser->buf_len); /* reset buffer length and flags. */ lparser->buf_len = 0; FLAG_RM_BUF(lparser->flags); FLAG_RM_HFIELD(lparser->flags); return 0; }
static int lhp_method(lua_State* L) { lhttp_parser* lparser = check_parser(L, 1); switch(lparser->parser.method) { case HTTP_DELETE: lua_pushliteral(L, "DELETE"); break; case HTTP_GET: lua_pushliteral(L, "GET"); break; case HTTP_HEAD: lua_pushliteral(L, "HEAD"); break; case HTTP_POST: lua_pushliteral(L, "POST"); break; case HTTP_PUT: lua_pushliteral(L, "PUT"); break; case HTTP_CONNECT: lua_pushliteral(L, "CONNECT"); break; case HTTP_OPTIONS: lua_pushliteral(L, "OPTIONS"); break; case HTTP_TRACE: lua_pushliteral(L, "TRACE"); break; case HTTP_COPY: lua_pushliteral(L, "COPY"); break; case HTTP_LOCK: lua_pushliteral(L, "LOCK"); break; case HTTP_MKCOL: lua_pushliteral(L, "MKCOL"); break; case HTTP_MOVE: lua_pushliteral(L, "MOVE"); break; case HTTP_PROPFIND: lua_pushliteral(L, "PROPFIND"); break; case HTTP_PROPPATCH: lua_pushliteral(L, "PROPPATCH"); break; case HTTP_UNLOCK: lua_pushliteral(L, "UNLOCK"); break; default: lua_pushnumber(L, lparser->parser.method); } return 1; }
int parse( Parser * parser, FileBuf * buf, FILE * out ) { if( !parser || !out ) return 0; Token cur_tk = new_tok(); for(;;) { cur_tk = get_token( buf->head ); if( cur_tk.type == NOTOKEN || cur_tk.type == EOF_TOK ) { break; } if( cur_tk.type == EOL_TOK ) { ++parser->cur_line; } if( cur_tk.type == PREPROC || cur_tk.type == MUL_COMMENT || cur_tk.type == STR_LIT ) { parser->cur_line += how_much_eols( &cur_tk ); } if( !handle_token( &cur_tk, parser, out ) ) return 0; if( cur_tk.type != EOL_TOK ) { parser->prev_tk = cur_tk; } buf->head = cur_tk.end; #ifdef DEBUG_ON check_parser( parser ); #endif // DEBUG_ON } return 1; }
static int lhp_status_code(lua_State* L) { lhttp_parser* lparser = check_parser(L, 1); lua_pushnumber(L, lparser->parser.status_code); return 1; }
static int lhp_version(lua_State* L) { lhttp_parser* lparser = check_parser(L, 1); lua_pushnumber(L, lparser->parser.http_major); lua_pushnumber(L, lparser->parser.http_minor); return 2; }
static int lhp_is_upgrade(lua_State* L) { lhttp_parser* lparser = check_parser(L, 1); lua_pushboolean(L, lparser->parser.upgrade); return 1; }
static int lhp_should_keep_alive(lua_State* L) { lhttp_parser* lparser = check_parser(L, 1); lua_pushboolean(L, http_should_keep_alive(&lparser->parser)); return 1; }