Skip to content
This repository has been archived by the owner on Jun 30, 2021. It is now read-only.

evhtp_set_gencb don't work #51

Closed
vvromanov opened this issue Oct 31, 2017 · 4 comments
Closed

evhtp_set_gencb don't work #51

vvromanov opened this issue Oct 31, 2017 · 4 comments
Assignees
Milestone

Comments

@vvromanov
Copy link
Contributor

Details

Steps or code to reproduce the problem.

evhtp_set_gencb don't work. Instead first evhtp_set_cb callback called.

Example code (if applicable)

This is modified version of test_basic.c

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdint.h>
#include <errno.h>
#include <evhtp.h>

void
testcb(evhtp_request_t * req, void * a) {
    const char * str = a;

    evbuffer_add(req->buffer_out, str, strlen(str));
    evhtp_send_reply(req, EVHTP_RES_OK);
}

void
issue161cb(evhtp_request_t * req, void * a) {
    struct evbuffer * b = evbuffer_new();

    if (evhtp_request_get_proto(req) == EVHTP_PROTO_10) {
        evhtp_request_set_keepalive(req, 0);
    }

    evhtp_send_reply_start(req, EVHTP_RES_OK);

    evbuffer_add(b, "foo", 3);
    evhtp_send_reply_body(req, b);

    evbuffer_add(b, "bar\n\n", 5);
    evhtp_send_reply_body(req, b);

    evhtp_send_reply_end(req);

    evbuffer_free(b);
}

int
main(int argc, char ** argv) {
    evbase_t * evbase = event_base_new();
    evhtp_t  * htp    = evhtp_new(evbase, NULL);

    evhtp_set_cb(htp, "/simple/", testcb, "simple");
    evhtp_set_cb(htp, "/1/ping", testcb, "one");
    evhtp_set_cb(htp, "/1/ping.json", testcb, "two");
    evhtp_set_cb(htp, "/issue161", issue161cb, NULL);
// VVVVVVVVVVVVVVVVVVVVVVVVV
    evhtp_set_gencb(htp, testcb, "root");
// ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
#ifndef EVHTP_DISABLE_EVTHR
    evhtp_use_threads_wexit(htp, NULL, NULL, 8, NULL);
#endif
    evhtp_bind_socket(htp, "0.0.0.0", 8081, 2048);

    event_base_loop(evbase, 0);

    evhtp_unbind_socket(htp);
    evhtp_free(htp);
    event_base_free(evbase);

    return 0;
}

Version

Latest

@NathanFrench NathanFrench self-assigned this Oct 31, 2017
NathanFrench added a commit to NathanFrench/libevhtp that referenced this issue Oct 31, 2017
- previously htp__callback_find_()'s strncmp() for type_hash
  would use the path_len instead of the callbacks path length

  This is wrong: if you have a callback set for '/foo/', and you
  request '/', the evaluation was: strncmp("/foo/", "/", 1);

  Which will, of course, match "/foo/" since it's only looking
  for one byte.

  Instead, it should be: strncmp("/", "/foo/", 5) which would
  not take the true branch.

- evhtp_callback_new now correctly sets the length (which is now
  used in htp__callback_find_
@NathanFrench
Copy link
Collaborator

Neato, big fail here.

As seen in the referenced commit above, I think this is a fix.

Can you try the https://github.com/criticalstack/libevhtp/tree/issue51 branch?

NathanFrench added a commit that referenced this issue Nov 1, 2017
[Bugfix #51] htp__callback_find_ length check fix
@NathanFrench
Copy link
Collaborator

Merged into develop. Thank you very much for pointing out this incredibly silly bug.

It reminds me of a story from the book "Deep C Secrets". I cannot quote the entire thing, but the gist was this: There was a bug in Solaris's mail command. Any user that had an "s" as the second character of their name would never receive email. Turns out there was a command line parsing bug; It was treating the first character as a - and looking ahead at the s, thinking it was -s (or something like this).

@NathanFrench NathanFrench added this to the v1.2.13 milestone Nov 2, 2017
@vvromanov
Copy link
Contributor Author

Fixed!

@NathanFrench
Copy link
Collaborator

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

2 participants