1
0

url_parser.c 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  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. struct http_parser_url u;
  24. int len, connect, result;
  25. if (argc != 3) {
  26. printf("Syntax : %s connect|get url\n", argv[0]);
  27. return 1;
  28. }
  29. len = strlen(argv[2]);
  30. connect = strcmp("connect", argv[1]) == 0 ? 1 : 0;
  31. printf("Parsing %s, connect %d\n", argv[2], connect);
  32. http_parser_url_init(&u);
  33. result = http_parser_parse_url(argv[2], len, connect, &u);
  34. if (result != 0) {
  35. printf("Parse error : %d\n", result);
  36. return result;
  37. }
  38. printf("Parse ok, result : \n");
  39. dump_url(argv[2], &u);
  40. return 0;
  41. }