1
0

parsertrace.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. /* Copyright Joyent, Inc. and other Node contributors.
  2. *
  3. * Permission is hereby granted, free of charge, to any person obtaining a copy
  4. * of this software and associated documentation files (the "Software"), to
  5. * deal in the Software without restriction, including without limitation the
  6. * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  7. * sell copies of the Software, and to permit persons to whom the Software is
  8. * furnished to do so, subject to the following conditions:
  9. *
  10. * The above copyright notice and this permission notice shall be included in
  11. * all copies or substantial portions of the Software.
  12. *
  13. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  14. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  15. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  16. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  17. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  18. * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  19. * IN THE SOFTWARE.
  20. */
  21. /* Dump what the parser finds to stdout as it happen */
  22. #include "http_parser.h"
  23. #include <stdio.h>
  24. #include <stdlib.h>
  25. #include <string.h>
  26. int on_message_begin(http_parser* _) {
  27. (void)_;
  28. printf("\n***MESSAGE BEGIN***\n\n");
  29. return 0;
  30. }
  31. int on_headers_complete(http_parser* _) {
  32. (void)_;
  33. printf("\n***HEADERS COMPLETE***\n\n");
  34. return 0;
  35. }
  36. int on_message_complete(http_parser* _) {
  37. (void)_;
  38. printf("\n***MESSAGE COMPLETE***\n\n");
  39. return 0;
  40. }
  41. int on_url(http_parser* _, const char* at, size_t length) {
  42. (void)_;
  43. printf("Url: %.*s\n", (int)length, at);
  44. return 0;
  45. }
  46. int on_header_field(http_parser* _, const char* at, size_t length) {
  47. (void)_;
  48. printf("Header field: %.*s\n", (int)length, at);
  49. return 0;
  50. }
  51. int on_header_value(http_parser* _, const char* at, size_t length) {
  52. (void)_;
  53. printf("Header value: %.*s\n", (int)length, at);
  54. return 0;
  55. }
  56. int on_body(http_parser* _, const char* at, size_t length) {
  57. (void)_;
  58. printf("Body: %.*s\n", (int)length, at);
  59. return 0;
  60. }
  61. void usage(const char* name) {
  62. fprintf(stderr,
  63. "Usage: %s $type $filename\n"
  64. " type: -x, where x is one of {r,b,q}\n"
  65. " parses file as a Response, reQuest, or Both\n",
  66. name);
  67. exit(EXIT_FAILURE);
  68. }
  69. int main(int argc, char* argv[]) {
  70. enum http_parser_type file_type;
  71. if (argc != 3) {
  72. usage(argv[0]);
  73. }
  74. char* type = argv[1];
  75. if (type[0] != '-') {
  76. usage(argv[0]);
  77. }
  78. switch (type[1]) {
  79. /* in the case of "-", type[1] will be NUL */
  80. case 'r':
  81. file_type = HTTP_RESPONSE;
  82. break;
  83. case 'q':
  84. file_type = HTTP_REQUEST;
  85. break;
  86. case 'b':
  87. file_type = HTTP_BOTH;
  88. break;
  89. default:
  90. usage(argv[0]);
  91. }
  92. char* filename = argv[2];
  93. FILE* file = fopen(filename, "r");
  94. if (file == NULL) {
  95. perror("fopen");
  96. goto fail;
  97. }
  98. fseek(file, 0, SEEK_END);
  99. long file_length = ftell(file);
  100. if (file_length == -1) {
  101. perror("ftell");
  102. goto fail;
  103. }
  104. fseek(file, 0, SEEK_SET);
  105. char* data = malloc(file_length);
  106. if (fread(data, 1, file_length, file) != (size_t)file_length) {
  107. fprintf(stderr, "couldn't read entire file\n");
  108. free(data);
  109. goto fail;
  110. }
  111. http_parser_settings settings;
  112. memset(&settings, 0, sizeof(settings));
  113. settings.on_message_begin = on_message_begin;
  114. settings.on_url = on_url;
  115. settings.on_header_field = on_header_field;
  116. settings.on_header_value = on_header_value;
  117. settings.on_headers_complete = on_headers_complete;
  118. settings.on_body = on_body;
  119. settings.on_message_complete = on_message_complete;
  120. http_parser parser;
  121. http_parser_init(&parser, file_type);
  122. size_t nparsed = http_parser_execute(&parser, &settings, data, file_length);
  123. free(data);
  124. if (nparsed != (size_t)file_length) {
  125. fprintf(stderr,
  126. "Error: %s (%s)\n",
  127. http_errno_description(HTTP_PARSER_ERRNO(&parser)),
  128. http_errno_name(HTTP_PARSER_ERRNO(&parser)));
  129. goto fail;
  130. }
  131. return EXIT_SUCCESS;
  132. fail:
  133. fclose(file);
  134. return EXIT_FAILURE;
  135. }