The little things give you away... A collection of various small helper stuff
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

312 lines
10 KiB

  1. #define _GNU_SOURCE
  2. #include <ctype.h>
  3. #include <stdbool.h>
  4. #include <stdio.h>
  5. #include <stdlib.h>
  6. #include <string.h>
  7. #ifndef BUFSIZE
  8. #define BUFSIZE 1048576
  9. #endif
  10. #define STATE_HEADERS 0
  11. #define STATE_BODY 1 // Body with a Content-Length header
  12. #define STATE_CHUNK_LINE 2
  13. #define STATE_CHUNK_CONTENTS 3
  14. #ifdef DEBUG
  15. #define DEBUG_PRINTF(...) do { fprintf(stderr, __VA_ARGS__); } while (false)
  16. #else
  17. #define DEBUG_PRINTF(...) do {} while (false)
  18. #endif
  19. int main(int argc, char* argv[]) {
  20. // Read stdin, decode HTTP responses, dump all bodies to stdout.
  21. // stdin may contain an extra 'URL LENGTH\n' line before each response (--meta output from warc-dump-responses).
  22. // One LF is inserted at the end of each response to ensure that a new response always begins on a new line.
  23. // If --html-fake-base is provided and the input contains URL data, every HTML response (Content-Type: text/html header) is prefixed with one line containing a fake <base> tag: <base href="URL">. The line is terminated with a LF.
  24. // Headers and chunk lines must fit into BUFSIZE.
  25. // Does not fully comply with the HTTP spec. For example, headers must be capitalised canonically, and continuation lines are unsupported.
  26. char buf[2 * BUFSIZE];
  27. size_t n;
  28. int state = STATE_HEADERS;
  29. char* bufp;
  30. char* m0;
  31. char* m1;
  32. char* eoh;
  33. long int nscan;
  34. size_t bytes_read;
  35. size_t length;
  36. bool html_fake_base = false;
  37. char* url = NULL; // Warning, pointer is only valid within the STATE_HEADERS block below.
  38. size_t urllen;
  39. if (argc == 2 && strcmp(argv[1], "--html-fake-base") == 0) {
  40. html_fake_base = true;
  41. }
  42. while ((n = fread(buf, 1, BUFSIZE, stdin)) > 0) {
  43. bufp = buf;
  44. checkstate:
  45. DEBUG_PRINTF("Have %zu bytes of buffer (at %p)\n", n, (void*)bufp);
  46. DEBUG_PRINTF("Beginning of buffer: ");
  47. for (int i = 0; i < (n > 64 ? 64 : n); ++i) DEBUG_PRINTF(isprint(*(bufp + i)) ? "%c" : "\\x%02x", *(bufp + i) & 0xFF);
  48. DEBUG_PRINTF("\n");
  49. if (n == 0) {
  50. break;
  51. }
  52. DEBUG_PRINTF("State: %d\n", state);
  53. if (state == STATE_HEADERS) {
  54. if (n < 9) {
  55. fprintf(stderr, "Error: too little data before HTTP headers\n");
  56. return 1;
  57. }
  58. // Handle optional URL + Length line
  59. url = NULL;
  60. if (memcmp(bufp, "HTTP/1.1 ", 9) != 0 && memcmp(bufp, "HTTP/1.0 ", 9) != 0) {
  61. DEBUG_PRINTF("No HTTP header, looking for URL line\n");
  62. m0 = memmem(bufp, n, "\n", 1);
  63. if (!m0 || m0 == bufp) {
  64. fprintf(stderr, "Error: expected HTTP headers or URL line, got neither\n");
  65. return 1;
  66. }
  67. m1 = m0;
  68. // Skip over length field, which we don't need.
  69. --m0;
  70. while (m0 > bufp && '0' <= *m0 && *m0 <= '9') --m0;
  71. if (*m0 != ' ') {
  72. fprintf(stderr, "Error: URL line has unexpected format\n");
  73. return 1;
  74. }
  75. // Rest must now be the URL; check that there is a scheme and no CR, LF, or whitespace.
  76. url = bufp;
  77. urllen = m0 - bufp;
  78. if (!memmem(url, urllen, "://", 3)) {
  79. fprintf(stderr, "Error: URL line contains no scheme\n");
  80. return 1;
  81. }
  82. m0 = url;
  83. while (m0 < bufp + urllen && *m0 != '\r' && *m0 != '\n' && *m0 != ' ' && *m0 != '\t') ++m0;
  84. if (m0 != bufp + urllen) {
  85. fprintf(stderr, "Error: URL contains CR, LF, or whitespace\n");
  86. return 1;
  87. }
  88. DEBUG_PRINTF("Found URL: ");
  89. for (int i = 0; i < (urllen > 64 ? 64 : urllen); ++i) DEBUG_PRINTF(isprint(*(url + i)) ? "%c" : "\\x%02x", *(url + i) & 0xFF);
  90. if (urllen > 64) DEBUG_PRINTF("<...>");
  91. DEBUG_PRINTF("\n");
  92. // Skip over URL line and continue processing below
  93. DEBUG_PRINTF("Adjusting buffer pointer and n by %zu\n", m1 + 1 - bufp);
  94. n = n - (m1 + 1 - bufp);
  95. bufp = m1 + 1;
  96. }
  97. if (html_fake_base && !url) {
  98. fprintf(stderr, "Error: --html-fake-base requires URL lines\n");
  99. return 1;
  100. }
  101. if (n < 9) {
  102. fprintf(stderr, "Error: too little data before HTTP headers\n");
  103. return 1;
  104. }
  105. if (memcmp(bufp, "HTTP/1.1 ", 9) == 0 || memcmp(bufp, "HTTP/1.0 ", 9) == 0) {
  106. // Got some headers; find transfer encoding, content length, and end of headers
  107. eoh = memmem(bufp, n, "\r\n\r\n", 4);
  108. if (!eoh) {
  109. fprintf(stderr, "Error: end of headers not found\n");
  110. return 1;
  111. }
  112. eoh += 4;
  113. DEBUG_PRINTF("Response body begins at %p (offset %zu)\n", (void*)eoh, eoh - bufp);
  114. m0 = memmem(bufp, n, "\r\nContent-Length:", 17);
  115. if (m0 && m0 < eoh) {
  116. DEBUG_PRINTF("Found Content-Length header at %p (offset %zu)\n", (void*)(m0 + 2), m0 + 2 - bufp);
  117. m1 = memmem(m0 + 1, n - (m0 + 1 - bufp), "\r\n", 2);
  118. if (!m1) {
  119. fprintf(stderr, "Error: CRLF after Content-Length missing\n");
  120. return 1;
  121. }
  122. m0 += 17;
  123. while (m0 < bufp + n && (*m0 == ' ' || *m0 == '\t')) ++m0;
  124. if (sscanf(m0, "%zu%ln", &length, &nscan) <= 0) {
  125. fprintf(stderr, "Error: invalid Content-Length\n");
  126. return 1;
  127. }
  128. if (nscan < 0) {
  129. fprintf(stderr, "Error: negative nscan\n");
  130. return 1;
  131. }
  132. if (nscan > n - (m0 - bufp)) {
  133. fprintf(stderr, "Error: buffer overread\n");
  134. return 1;
  135. }
  136. m0 += nscan;
  137. while (m0 < bufp + n && (*m0 == ' ' || *m0 == '\t')) ++m0;
  138. if (m0 != m1) {
  139. fprintf(stderr, "Error: invalid Content-Length (noise before EOL)\n");
  140. return 1;
  141. }
  142. DEBUG_PRINTF("Content length: %zu\n", length);
  143. state = STATE_BODY;
  144. } else {
  145. m0 = memmem(bufp, n, "\r\nTransfer-Encoding:", 20);
  146. if (!m0 || m0 >= eoh) {
  147. fprintf(stderr, "Error: Content-Length and Transfer-Encoding missing\n");
  148. return 1;
  149. }
  150. DEBUG_PRINTF("Found Transfer-Encoding header at %p (offset %zu)\n", (void*)(m0 + 2), m0 + 2 - bufp);
  151. m1 = memmem(m0 + 1, n - (m0 + 1 - bufp), "\r\n", 2);
  152. if (!m1 || m1 >= eoh - 2) {
  153. fprintf(stderr, "Error: CRLF after Transfer-Encoding missing\n");
  154. return 1;
  155. }
  156. m0 += 20;
  157. while (m0 < bufp + n && (*m0 == ' ' || *m0 == '\t')) ++m0;
  158. if (memcmp(m0, "chunked", 7) != 0) {
  159. fprintf(stderr, "Error: unsupported Transfer-Encoding\n");
  160. return 1;
  161. }
  162. m0 += 7;
  163. while (m0 < bufp + n && (*m0 == ' ' || *m0 == '\t')) ++m0;
  164. if (m0 != m1) {
  165. fprintf(stderr, "Error: unsupported Transfer-Encoding\n");
  166. return 1;
  167. }
  168. DEBUG_PRINTF("Chunked transfer encoding\n");
  169. state = STATE_CHUNK_LINE;
  170. }
  171. if (html_fake_base) {
  172. m0 = memmem(bufp, n, "\r\nContent-Type:", 15);
  173. if (m0 && m0 < eoh) {
  174. DEBUG_PRINTF("Found Content-Type header at %p (offset %zu)\n", (void*)(m0 + 2), m0 + 2 - bufp);
  175. m1 = memmem(m0 + 1, n - (m0 + 1 - bufp), "\r\n", 2);
  176. if (!m1) {
  177. fprintf(stderr, "Error: CRLF after Content-Type missing\n");
  178. return 1;
  179. }
  180. m0 += 15;
  181. while (m0 < bufp + n && (*m0 == ' ' || *m0 == '\t')) ++m0;
  182. if (memcmp(m0, "text/html", 9) == 0) {
  183. DEBUG_PRINTF("Is HTML response, inserting fake base tag\n");
  184. fprintf(stdout, "<base href=\"");
  185. fwrite(url, 1, urllen, stdout);
  186. fprintf(stdout, "\">\n");
  187. }
  188. }
  189. }
  190. DEBUG_PRINTF("Adjusting buffer pointer and n by %zu\n", eoh - bufp);
  191. n = n - (eoh - bufp);
  192. bufp = eoh;
  193. bytes_read = 0;
  194. goto checkstate;
  195. } else {
  196. fprintf(stderr, "Error: expected header line, got something else\n");
  197. return 1;
  198. }
  199. } else if (state == STATE_BODY || state == STATE_CHUNK_CONTENTS) {
  200. if (length + (state == STATE_BODY ? 1 : 2) - bytes_read > n) {
  201. // Only got part of the body
  202. DEBUG_PRINTF("Partial body\n");
  203. DEBUG_PRINTF("Copying %zu bytes to stdout\n", n);
  204. fwrite(bufp, 1, n, stdout);
  205. bytes_read += n;
  206. DEBUG_PRINTF("%zu of %zu bytes from this response written\n", bytes_read, length);
  207. } else {
  208. // Remainder of the response is in the buffer. Same logic as above for small records fitting in the buffer with the headers.
  209. DEBUG_PRINTF("Full body\n");
  210. DEBUG_PRINTF("Copying %zu bytes to stdout\n", length - bytes_read);
  211. fwrite(bufp, 1, length - bytes_read, stdout);
  212. fprintf(stdout, "\n");
  213. if (state == STATE_CHUNK_CONTENTS && *(bufp + length - bytes_read) == '\r') {
  214. // Stupid hack to enforce the CRLF
  215. ++length;
  216. }
  217. if (memcmp(bufp + length - bytes_read, "\n", 1) != 0) {
  218. fprintf(stderr, "Error: end of HTTP body not found\n");
  219. return 1;
  220. }
  221. DEBUG_PRINTF("Adjusting buffer pointer and n by %zu\n", length + 1 - bytes_read);
  222. n = n - (length + 1 - bytes_read);
  223. bufp = bufp + length + 1 - bytes_read;
  224. if (n < BUFSIZE) {
  225. DEBUG_PRINTF("Buffer too small (%zu bytes), moving and refilling\n", n);
  226. memmove(buf, bufp, n);
  227. bufp = buf;
  228. n += fread(buf + n, 1, BUFSIZE, stdin);
  229. }
  230. if (state == STATE_BODY) {
  231. state = STATE_HEADERS;
  232. } else {
  233. state = STATE_CHUNK_LINE;
  234. }
  235. goto checkstate;
  236. }
  237. } else if (state == STATE_CHUNK_LINE) {
  238. m1 = memmem(bufp, n, "\r\n", 2);
  239. if (!m1) {
  240. fprintf(stderr, "Error: chunk line EOL missing\n");
  241. return 1;
  242. }
  243. m0 = bufp;
  244. while (m0 < bufp + n && (*m0 == ' ' || *m0 == '\t')) ++m0;
  245. if (sscanf(m0, "%zx%ln", &length, &nscan) <= 0) {
  246. fprintf(stderr, "Error: invalid chunk length\n");
  247. return 1;
  248. }
  249. if (nscan < 0) {
  250. fprintf(stderr, "Error: negative nscan\n");
  251. return 1;
  252. }
  253. if (nscan > n - (m0 - bufp)) {
  254. fprintf(stderr, "Error: buffer overread\n");
  255. return 1;
  256. }
  257. m0 += nscan;
  258. while (m0 < m1 && (*m0 == ' ' || *m0 == '\t')) ++m0;
  259. if (*m0 != ';' && m0 != m1) {
  260. fprintf(stderr, "Error: invalid Content-Length (noise before EOL)\n");
  261. return 1;
  262. }
  263. DEBUG_PRINTF("Chunk length: %zu bytes\n", length);
  264. DEBUG_PRINTF("Adjusting buffer pointer and n by %zu\n", m1 + 2 - bufp);
  265. n = n - (m1 + 2 - bufp);
  266. bufp = m1 + 2;
  267. if (length == 0) {
  268. // End of response, must be followed by CRLF + LF
  269. if (n < 3) {
  270. fprintf(stderr, "Error: buffer exhausted while looking for empty chunk CRLF\n");
  271. return 1;
  272. }
  273. if (*(m1 + 2) != '\r' || *(m1 + 3) != '\n' || *(m1 + 4) != '\n') {
  274. fprintf(stderr, "Error: end of HTTP body not found\n");
  275. return 1;
  276. }
  277. n -= 3;
  278. bufp += 3;
  279. state = STATE_HEADERS;
  280. } else {
  281. state = STATE_CHUNK_CONTENTS;
  282. }
  283. if (n < BUFSIZE) {
  284. DEBUG_PRINTF("Buffer too small (%zu bytes), moving and refilling\n", n);
  285. memmove(buf, bufp, n);
  286. bufp = buf;
  287. n += fread(buf + n, 1, BUFSIZE, stdin);
  288. }
  289. goto checkstate;
  290. }
  291. }
  292. if (state != STATE_HEADERS) {
  293. fprintf(stderr, "Error: incomplete body at the end of input\n");
  294. return 1;
  295. }
  296. }