parsing - Fast(est) way to get X-Forwarded-for in header with G-Wan? -
since non-rfc header field isn't in http_env nor http_t struct, wrote snippet :
//get reply buffer xbuf_t *reply = get_reply(argv); // init & create json buffer xbuf_t json_buf; xbuf_init(&json_buf); jsn_t *params = jsn_add_node(0, "params"); [...] http_t *http = (http_t*)get_env(argv, http_headers); xbuf_t *read_buf = (xbuf_t*)get_env(argv, read_xbuf); [...] // add remote ip (from x-forwarded-for or remote_addr) char *xforward = xbuf_findstr(read_buf, "x-forwarded-for:"); if(xforward) { jsn_add_string(params, "ip", xforward); } else { jsn_add_string(params, "ip", get_env(argv, remote_addr)); }
the json part is, of course, part of application , irrelevant question. think it's right way or xbuf_findstr() inefficient , there faster solution (without using asm or cryptic code) ?
thank :)
edit : wow, forgot strok() part rid of first field, want ip adress of course.
edit2 : patched version strtok
// add remote ip (from x-forwarded-for or remote_addr) char *xforward = xbuf_findstr(read_buf, "x-forwarded-for: "); if(xforward) { gc_init(argv, 1024); char *copy = gc_malloc(argv, strlen(xforward)+ 1); memcpy(copy, xforward, strlen(xforward) + 1); strtok(copy, ":"); char *ip = strtok(0,"\r"); while (*ip == ' ') ip++; // trim leading space jsn_add_string(params, "ip", ip); } else { jsn_add_string(params, "ip", get_env(argv, remote_addr)); }
fastest way?
if code executed after http request parsing g-wan state start searching "x-forwarded-for: "
closest place (another http header) instead of doing start of request.
there no way faster xbuf_findstr()
. string search methods faster others depending on length of input data, , not significant here.
"x-forwarded-for: "
parsed g-wan in past have implemented proxy feature in g-wan, , since "x-forwarded-for: "
header can fake, removed parser.
Comments
Post a Comment