url_parser.c 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. #include "http_parser.h"
  2. #include <stdio.h>
  3. #include <string.h>
  4. void
  5. dump_url (const char *url, const struct http_parser_url *u)
  6. {
  7. unsigned int i;
  8. printf("\tfield_set: 0x%x, port: %u\n", u->field_set, u->port);
  9. for (i = 0; i < UF_MAX; i++) {
  10. if ((u->field_set & (1 << i)) == 0) {
  11. printf("\tfield_data[%u]: unset\n", i);
  12. continue;
  13. }
  14. printf("\tfield_data[%u]: off: %u len: %u part: \"%.*s\n",
  15. i,
  16. u->field_data[i].off,
  17. u->field_data[i].len,
  18. u->field_data[i].len,
  19. url + u->field_data[i].off);
  20. }
  21. }
  22. int main(int argc, char ** argv) {
  23. if (argc != 3) {
  24. printf("Syntax : %s connect|get url\n", argv[0]);
  25. return 1;
  26. }
  27. struct http_parser_url u;
  28. int len = strlen(argv[2]);
  29. int connect = strcmp("connect", argv[1]) == 0 ? 1 : 0;
  30. printf("Parsing %s, connect %d\n", argv[2], connect);
  31. int result = http_parser_parse_url(argv[2], len, connect, &u);
  32. if (result != 0) {
  33. printf("Parse error : %d\n", result);
  34. return result;
  35. }
  36. printf("Parse ok, result : \n");
  37. dump_url(argv[2], &u);
  38. return 0;
  39. }