url_parser.c 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  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. result = http_parser_parse_url(argv[2], len, connect, &u);
  33. if (result != 0) {
  34. printf("Parse error : %d\n", result);
  35. return result;
  36. }
  37. printf("Parse ok, result : \n");
  38. dump_url(argv[2], &u);
  39. return 0;
  40. }