1 |
/* |
/* |
2 |
* HTTP Header Lint |
* HTTP Header Lint |
3 |
* Licensed under the same license as Curl |
* Licensed under the MIT License |
4 |
* http://curl.haxx.se/docs/copyright.html |
* http://www.opensource.org/licenses/mit-license |
5 |
* Copyright 2003 James Bursa <bursa@users.sourceforge.net> |
* Copyright 2004 James Bursa <bursa@users.sourceforge.net> |
6 |
*/ |
*/ |
7 |
|
|
8 |
/* |
/* |
32 |
|
|
33 |
|
|
34 |
bool start; |
bool start; |
35 |
|
bool html = false; |
36 |
CURL *curl; |
CURL *curl; |
37 |
int status_code; |
int status_code; |
38 |
char error_buffer[CURL_ERROR_SIZE]; |
char error_buffer[CURL_ERROR_SIZE]; |
84 |
void header_via(const char *s); |
void header_via(const char *s); |
85 |
void header_set_cookie(const char *s); |
void header_set_cookie(const char *s); |
86 |
void die(const char *error); |
void die(const char *error); |
|
void warning(const char *message); |
|
|
void error(const char *message); |
|
87 |
void print(const char *s, size_t len); |
void print(const char *s, size_t len); |
88 |
void lookup(const char *key); |
void lookup(const char *key); |
89 |
|
|
128 |
*/ |
*/ |
129 |
int main(int argc, char *argv[]) |
int main(int argc, char *argv[]) |
130 |
{ |
{ |
131 |
int i; |
int i = 1; |
132 |
|
|
133 |
if (argc < 2) |
if (argc < 2) |
134 |
die("Usage: httplint url [url ...]"); |
die("Usage: httplint [--html] url [url ...]"); |
135 |
|
|
136 |
init(); |
init(); |
137 |
|
|
138 |
for (i = 1; i != argc; i++) |
if (1 < argc && strcmp(argv[1], "--html") == 0) { |
139 |
|
html = true; |
140 |
|
i++; |
141 |
|
} |
142 |
|
|
143 |
|
for (; i != argc; i++) |
144 |
check_url(argv[i]); |
check_url(argv[i]); |
145 |
|
|
146 |
curl_global_cleanup(); |
curl_global_cleanup(); |
267 |
for (i = 0; i != sizeof header_table / sizeof header_table[0]; i++) |
for (i = 0; i != sizeof header_table / sizeof header_table[0]; i++) |
268 |
header_table[i].count = 0; |
header_table[i].count = 0; |
269 |
|
|
270 |
printf("Checking URL %s\n", url); |
if (!html) |
271 |
if (strncmp(url, "http", 4)) |
printf("Checking URL %s\n", url); |
272 |
warning("this is not an http or https url"); |
if (strncmp(url, "http", 4)) { |
273 |
|
if (html) |
274 |
|
printf("<p class='warning'>"); |
275 |
|
printf("Warning: this is not an http or https url"); |
276 |
|
if (html) |
277 |
|
printf("</p>"); |
278 |
|
printf("\n"); |
279 |
|
} |
280 |
|
|
281 |
if (curl_easy_setopt(curl, CURLOPT_URL, url)) |
if (curl_easy_setopt(curl, CURLOPT_URL, url)) |
282 |
die("Failed to set curl options"); |
die("Failed to set curl options"); |
283 |
|
|
284 |
|
if (html) |
285 |
|
printf("<ul>\n"); |
286 |
code = curl_easy_perform(curl); |
code = curl_easy_perform(curl); |
287 |
|
if (html) |
288 |
|
printf("</ul>\n"); |
289 |
if (code != CURLE_OK && code != CURLE_WRITE_ERROR) { |
if (code != CURLE_OK && code != CURLE_WRITE_ERROR) { |
290 |
error(error_buffer); |
if (html) |
291 |
|
printf("<p class='error'>"); |
292 |
|
printf("Error: "); |
293 |
|
print(error_buffer, strlen(error_buffer)); |
294 |
|
printf("."); |
295 |
|
if (html) |
296 |
|
printf("</p>"); |
297 |
|
printf("\n"); |
298 |
return; |
return; |
299 |
} else { |
} else { |
300 |
printf("\n"); |
printf("\n"); |
301 |
|
if (html) |
302 |
|
printf("<ul>"); |
303 |
for (i = 0; i != sizeof header_table / sizeof header_table[0]; i++) { |
for (i = 0; i != sizeof header_table / sizeof header_table[0]; i++) { |
304 |
if (header_table[i].count == 0 && header_table[i].missing) |
if (header_table[i].count == 0 && header_table[i].missing) |
305 |
lookup(header_table[i].missing); |
lookup(header_table[i].missing); |
309 |
r = regexec(&re_ugly, url, 0, 0, 0); |
r = regexec(&re_ugly, url, 0, 0, 0); |
310 |
if (r) |
if (r) |
311 |
lookup("ugly"); |
lookup("ugly"); |
312 |
|
|
313 |
|
if (html) |
314 |
|
printf("</ul>"); |
315 |
} |
} |
316 |
|
|
317 |
|
|
325 |
|
|
326 |
UNUSED(stream); |
UNUSED(stream); |
327 |
|
|
328 |
printf("* "); |
printf(html ? "<li><code>" : "* "); |
329 |
print(ptr, size); |
print(ptr, size); |
330 |
printf("\n"); |
printf(html ? "</code><ul>" : "\n"); |
331 |
|
|
332 |
if (size < 2 || ptr[size - 2] != 13 || ptr[size - 1] != 10) { |
if (size < 2 || ptr[size - 2] != 13 || ptr[size - 1] != 10) { |
333 |
lookup("notcrlf"); |
lookup("notcrlf"); |
334 |
|
if (html) |
335 |
|
printf("</ul></li>\n"); |
336 |
return size; |
return size; |
337 |
} |
} |
338 |
if (sizeof s <= size) { |
if (sizeof s <= size) { |
339 |
warning("header too long: ignored\n"); |
lookup("headertoolong"); |
340 |
|
if (html) |
341 |
|
printf("</ul></li>\n"); |
342 |
return size; |
return size; |
343 |
} |
} |
344 |
strncpy(s, ptr, size); |
strncpy(s, ptr, size); |
349 |
|
|
350 |
if (s[0] == 0) { |
if (s[0] == 0) { |
351 |
/* empty header indicates end of headers */ |
/* empty header indicates end of headers */ |
352 |
puts("End of headers."); |
lookup("endofheaders"); |
353 |
|
if (html) |
354 |
|
printf("</ul></li>\n"); |
355 |
return 0; |
return 0; |
356 |
|
|
357 |
} else if (start) { |
} else if (start) { |
369 |
check_header(name, skip_lws(value)); |
check_header(name, skip_lws(value)); |
370 |
} |
} |
371 |
|
|
372 |
|
if (html) |
373 |
|
printf("</ul></li>\n"); |
374 |
return size; |
return size; |
375 |
} |
} |
376 |
|
|
460 |
int len = strlen(s); |
int len = strlen(s); |
461 |
regmatch_t pmatch[20]; |
regmatch_t pmatch[20]; |
462 |
|
|
463 |
|
tm->tm_isdst = 0; |
464 |
|
tm->tm_gmtoff = 0; |
465 |
|
tm->tm_zone = "GMT"; |
466 |
|
|
467 |
if (len == 29) { |
if (len == 29) { |
468 |
/* RFC 1123 */ |
/* RFC 1123 */ |
469 |
r = regexec(&re_rfc1123, s, 20, pmatch, 0); |
r = regexec(&re_rfc1123, s, 20, pmatch, 0); |
606 |
do { |
do { |
607 |
r = regexec(preg, s, 20, pmatch, 0); |
r = regexec(preg, s, 20, pmatch, 0); |
608 |
if (r) { |
if (r) { |
609 |
|
if (html) |
610 |
|
printf("<li class='error'>"); |
611 |
printf(" Failed to match list item %i\n", items + 1); |
printf(" Failed to match list item %i\n", items + 1); |
612 |
|
if (html) |
613 |
|
printf("</li>\n"); |
614 |
return false; |
return false; |
615 |
} |
} |
616 |
|
|
623 |
if (*s == 0) |
if (*s == 0) |
624 |
break; |
break; |
625 |
if (*s != ',') { |
if (*s != ',') { |
626 |
|
if (html) |
627 |
|
printf("<li class='error'>"); |
628 |
printf(" Expecting , after list item %i\n", items); |
printf(" Expecting , after list item %i\n", items); |
629 |
|
if (html) |
630 |
|
printf("</li>\n"); |
631 |
return false; |
return false; |
632 |
} |
} |
633 |
while (*s == ',') |
while (*s == ',') |
635 |
} while (*s != 0); |
} while (*s != 0); |
636 |
|
|
637 |
if (items < n || m < items) { |
if (items < n || m < items) { |
638 |
|
if (html) |
639 |
|
printf("<li class='error'>"); |
640 |
printf(" %i items in list, but there should be ", items); |
printf(" %i items in list, but there should be ", items); |
641 |
if (m == UINT_MAX) |
if (m == UINT_MAX) |
642 |
printf("at least %i\n", n); |
printf("at least %i\n", n); |
643 |
else |
else |
644 |
printf("between %i and %i\n", n, m); |
printf("between %i and %i\n", n, m); |
645 |
|
if (html) |
646 |
|
printf("</li>\n"); |
647 |
return false; |
return false; |
648 |
} |
} |
649 |
|
|
713 |
(int (*)(const void *, const void *)) strcasecmp); |
(int (*)(const void *, const void *)) strcasecmp); |
714 |
|
|
715 |
if (!dir) { |
if (!dir) { |
716 |
printf(" Cache-Control directive '%s':\n", name); |
if (html) |
717 |
|
printf("<li class='warning'>"); |
718 |
|
printf(" Cache-Control directive '"); |
719 |
|
print(name, strlen(name)); |
720 |
|
printf("':\n"); |
721 |
|
if (html) |
722 |
|
printf("</li>\n"); |
723 |
lookup("unknowncachecont"); |
lookup("unknowncachecont"); |
724 |
} |
} |
725 |
} |
} |
764 |
sizeof content_coding_list[0], |
sizeof content_coding_list[0], |
765 |
(int (*)(const void *, const void *)) strcasecmp); |
(int (*)(const void *, const void *)) strcasecmp); |
766 |
if (!dir) { |
if (!dir) { |
767 |
|
if (html) |
768 |
|
printf("<li class='warning'>"); |
769 |
printf(" Content-Encoding '%s':\n", name); |
printf(" Content-Encoding '%s':\n", name); |
770 |
|
if (html) |
771 |
|
printf("</li>\n"); |
772 |
lookup("unknowncontenc"); |
lookup("unknowncontenc"); |
773 |
} |
} |
774 |
} |
} |
983 |
sizeof transfer_coding_list[0], |
sizeof transfer_coding_list[0], |
984 |
(int (*)(const void *, const void *)) strcasecmp); |
(int (*)(const void *, const void *)) strcasecmp); |
985 |
if (!dir) { |
if (!dir) { |
986 |
|
if (html) |
987 |
|
printf("<li class='warning'>"); |
988 |
printf(" Transfer-Encoding '%s':\n", name); |
printf(" Transfer-Encoding '%s':\n", name); |
989 |
|
if (html) |
990 |
|
printf("</li>\n"); |
991 |
lookup("unknowntransenc"); |
lookup("unknowntransenc"); |
992 |
} |
} |
993 |
} |
} |
1038 |
lookup("cookiebadnameval"); |
lookup("cookiebadnameval"); |
1039 |
ok = false; |
ok = false; |
1040 |
} |
} |
1041 |
|
|
1042 |
if (!semi) |
if (!semi) |
1043 |
return; |
return; |
1044 |
|
|
1051 |
else |
else |
1052 |
s2 = s; |
s2 = s; |
1053 |
|
|
1054 |
if (strncmp(s2, "expires=", 8) == 0) { |
if (strncasecmp(s2, "expires=", 8) == 0) { |
1055 |
s2 += 8; |
s2 += 8; |
1056 |
r = regexec(&re_cookie_expires, s2, 20, pmatch, 0); |
r = regexec(&re_cookie_expires, s2, 20, pmatch, 0); |
1057 |
if (r == 0) { |
if (r == 0) { |
1074 |
lookup("cookiebaddate"); |
lookup("cookiebaddate"); |
1075 |
ok = false; |
ok = false; |
1076 |
} |
} |
1077 |
} else if (strncmp(s2, "domain=", 7) == 0) { |
} else if (strncasecmp(s2, "domain=", 7) == 0) { |
1078 |
} else if (strncmp(s2, "path=", 5) == 0) { |
} else if (strncasecmp(s2, "path=", 5) == 0) { |
1079 |
if (s2[5] != '/') { |
if (s2[5] != '/') { |
1080 |
lookup("cookiebadpath"); |
lookup("cookiebadpath"); |
1081 |
ok = false; |
ok = false; |
1082 |
} |
} |
1083 |
} else if (strcmp(s, "secure") == 0) { |
} else if (strcasecmp(s, "secure") == 0) { |
1084 |
} else { |
} else { |
1085 |
|
if (html) |
1086 |
|
printf("<li class='warning'>"); |
1087 |
printf(" Set-Cookie field '%s':\n", s2); |
printf(" Set-Cookie field '%s':\n", s2); |
1088 |
|
if (html) |
1089 |
|
printf("</li>\n"); |
1090 |
lookup("cookieunknownfield"); |
lookup("cookieunknownfield"); |
1091 |
ok = false; |
ok = false; |
1092 |
} |
} |
1113 |
|
|
1114 |
|
|
1115 |
/** |
/** |
|
* Print a warning message. |
|
|
*/ |
|
|
void warning(const char *message) |
|
|
{ |
|
|
printf("Warning: %s\n", message); |
|
|
} |
|
|
|
|
|
|
|
|
/** |
|
|
* Print an error message. |
|
|
*/ |
|
|
void error(const char *message) |
|
|
{ |
|
|
printf("Error: %s\n", message); |
|
|
} |
|
|
|
|
|
|
|
|
/** |
|
1116 |
* Print a string which contains control characters. |
* Print a string which contains control characters. |
1117 |
*/ |
*/ |
1118 |
void print(const char *s, size_t len) |
void print(const char *s, size_t len) |
1119 |
{ |
{ |
1120 |
size_t i; |
size_t i; |
1121 |
for (i = 0; i != len; i++) { |
for (i = 0; i != len; i++) { |
1122 |
if (31 < s[i] && s[i] < 127) |
if (html && s[i] == '<') |
1123 |
|
printf("<"); |
1124 |
|
else if (html && s[i] == '>') |
1125 |
|
printf(">"); |
1126 |
|
else if (html && s[i] == '&') |
1127 |
|
printf("&"); |
1128 |
|
else if (31 < s[i] && s[i] < 127) |
1129 |
putchar(s[i]); |
putchar(s[i]); |
1130 |
else |
else { |
1131 |
|
if (html) |
1132 |
|
printf("<span class='cc'>"); |
1133 |
printf("[%.2x]", s[i]); |
printf("[%.2x]", s[i]); |
1134 |
|
if (html) |
1135 |
|
printf("</span>"); |
1136 |
|
} |
1137 |
} |
} |
1138 |
} |
} |
1139 |
|
|
1208 |
"will be deleted by browsers." }, |
"will be deleted by browsers." }, |
1209 |
{ "cookieunknownfield", "Warning: This is not a standard Set-Cookie " |
{ "cookieunknownfield", "Warning: This is not a standard Set-Cookie " |
1210 |
"field." }, |
"field." }, |
1211 |
|
{ "endofheaders", "End of headers." }, |
1212 |
{ "futurehttp", "Warning: I only understand HTTP/1.1. Check for a newer " |
{ "futurehttp", "Warning: I only understand HTTP/1.1. Check for a newer " |
1213 |
"version of this tool." }, |
"version of this tool." }, |
1214 |
{ "futurelastmod", "Error: The specified Last-Modified date-time is in " |
{ "futurelastmod", "Error: The specified Last-Modified date-time is in " |
1215 |
"the future." }, |
"the future." }, |
1216 |
|
{ "headertoolong", "Warning: Header too long: ignored." }, |
1217 |
{ "missingcolon", "Error: Headers must be of the form 'Name: value'." }, |
{ "missingcolon", "Error: Headers must be of the form 'Name: value'." }, |
1218 |
{ "missingcontenttype", "Warning: No Content-Type header was present. The " |
{ "missingcontenttype", "Warning: No Content-Type header was present. The " |
1219 |
"client will have to guess the media type or ask " |
"client will have to guess the media type or ask " |
1277 |
else |
else |
1278 |
s = key; |
s = key; |
1279 |
|
|
1280 |
printf(" "); |
if (html) { |
1281 |
x = 4; |
if (strncmp(s, "Warning:", 8) == 0) |
1282 |
while (*s) { |
printf("<li class='warning'>"); |
1283 |
spc = strchr(s, ' '); |
else if (strncmp(s, "Error:", 6) == 0) |
1284 |
if (!spc) |
printf("<li class='error'>"); |
1285 |
spc = s + strlen(s); |
else if (strncmp(s, "OK", 2) == 0) |
1286 |
if (75 < x + (spc - s)) { |
printf("<li class='ok'>"); |
|
printf("\n "); |
|
|
x = 4; |
|
|
} |
|
|
x += spc - s + 1; |
|
|
printf("%.*s ", spc - s, s); |
|
|
if (*spc) |
|
|
s = spc + 1; |
|
1287 |
else |
else |
1288 |
s = spc; |
printf("<li>"); |
1289 |
|
for (; *s; s++) { |
1290 |
|
if (strncmp(s, "http://", 7) == 0) { |
1291 |
|
spc = strchr(s, ' '); |
1292 |
|
printf("<a href='%.*s'>%.*s</a>", spc - s, s, spc - s, s); |
1293 |
|
s = spc; |
1294 |
|
} |
1295 |
|
switch (*s) { |
1296 |
|
case '<': printf("<"); break; |
1297 |
|
case '>': printf(">"); break; |
1298 |
|
case '&': printf("&"); break; |
1299 |
|
default: printf("%c", *s); break; |
1300 |
|
} |
1301 |
|
} |
1302 |
|
printf("</li>\n"); |
1303 |
|
|
1304 |
|
} else { |
1305 |
|
printf(" "); |
1306 |
|
x = 4; |
1307 |
|
while (*s) { |
1308 |
|
spc = strchr(s, ' '); |
1309 |
|
if (!spc) |
1310 |
|
spc = s + strlen(s); |
1311 |
|
if (75 < x + (spc - s)) { |
1312 |
|
printf("\n "); |
1313 |
|
x = 4; |
1314 |
|
} |
1315 |
|
x += spc - s + 1; |
1316 |
|
printf("%.*s ", spc - s, s); |
1317 |
|
if (*spc) |
1318 |
|
s = spc + 1; |
1319 |
|
else |
1320 |
|
s = spc; |
1321 |
|
} |
1322 |
|
printf("\n\n"); |
1323 |
} |
} |
|
printf("\n\n"); |
|
1324 |
} |
} |
1325 |
|
|