1
0

parsertrace.c 4.2 KB

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