/[james]/httplint/httplint.c
ViewVC logotype

Diff of /httplint/httplint.c

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 55 by james, Fri Feb 20 20:49:40 2004 UTC revision 56 by james, Fri Mar 12 21:54:02 2004 UTC
# Line 32  Line 32 
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];
# Line 83  void header_vary(const char *s); Line 84  void header_vary(const char *s);
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    
# Line 129  struct header_entry { Line 128  struct header_entry {
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();
# Line 263  void check_url(const char *url) Line 267  void check_url(const char *url)
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);
# Line 285  void check_url(const char *url) Line 309  void check_url(const char *url)
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    
# Line 298  size_t header_callback(char *ptr, size_t Line 325  size_t header_callback(char *ptr, size_t
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);
# Line 318  size_t header_callback(char *ptr, size_t Line 349  size_t header_callback(char *ptr, size_t
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) {
# Line 336  size_t header_callback(char *ptr, size_t Line 369  size_t header_callback(char *ptr, size_t
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    
# Line 567  bool parse_list(const char *s, regex_t * Line 602  bool parse_list(const char *s, regex_t *
602    do {    do {
603      r = regexec(preg, s, 20, pmatch, 0);      r = regexec(preg, s, 20, pmatch, 0);
604      if (r) {      if (r) {
605          if (html)
606            printf("<li class='error'>");
607        printf("    Failed to match list item %i\n", items + 1);        printf("    Failed to match list item %i\n", items + 1);
608          if (html)
609            printf("</li>\n");
610        return false;        return false;
611      }      }
612    
# Line 580  bool parse_list(const char *s, regex_t * Line 619  bool parse_list(const char *s, regex_t *
619      if (*s == 0)      if (*s == 0)
620        break;        break;
621      if (*s != ',') {      if (*s != ',') {
622          if (html)
623            printf("<li class='error'>");
624        printf("    Expecting , after list item %i\n", items);        printf("    Expecting , after list item %i\n", items);
625          if (html)
626            printf("</li>\n");
627        return false;        return false;
628      }      }
629      while (*s == ',')      while (*s == ',')
# Line 588  bool parse_list(const char *s, regex_t * Line 631  bool parse_list(const char *s, regex_t *
631    } while (*s != 0);    } while (*s != 0);
632    
633    if (items < n || m < items) {    if (items < n || m < items) {
634        if (html)
635          printf("<li class='error'>");
636      printf("    %i items in list, but there should be ", items);      printf("    %i items in list, but there should be ", items);
637      if (m == UINT_MAX)      if (m == UINT_MAX)
638        printf("at least %i\n", n);        printf("at least %i\n", n);
639      else      else
640        printf("between %i and %i\n", n, m);        printf("between %i and %i\n", n, m);
641        if (html)
642          printf("</li>\n");
643      return false;      return false;
644    }    }
645    
# Line 662  void header_cache_control_callback(const Line 709  void header_cache_control_callback(const
709        (int (*)(const void *, const void *)) strcasecmp);        (int (*)(const void *, const void *)) strcasecmp);
710    
711    if (!dir) {    if (!dir) {
712      printf("    Cache-Control directive '%s':\n", name);      if (html)
713          printf("<li class='warning'>");
714        printf("    Cache-Control directive '");
715        print(name, strlen(name));
716        printf("':\n");
717        if (html)
718          printf("</li>\n");
719      lookup("unknowncachecont");      lookup("unknowncachecont");
720    }    }
721  }  }
# Line 707  void header_content_encoding_callback(co Line 760  void header_content_encoding_callback(co
760        sizeof content_coding_list[0],        sizeof content_coding_list[0],
761        (int (*)(const void *, const void *)) strcasecmp);        (int (*)(const void *, const void *)) strcasecmp);
762    if (!dir) {    if (!dir) {
763        if (html)
764          printf("<li class='warning'>");
765      printf("    Content-Encoding '%s':\n", name);      printf("    Content-Encoding '%s':\n", name);
766        if (html)
767          printf("</li>\n");
768      lookup("unknowncontenc");      lookup("unknowncontenc");
769    }    }
770  }  }
# Line 922  void header_transfer_encoding_callback(c Line 979  void header_transfer_encoding_callback(c
979        sizeof transfer_coding_list[0],        sizeof transfer_coding_list[0],
980        (int (*)(const void *, const void *)) strcasecmp);        (int (*)(const void *, const void *)) strcasecmp);
981    if (!dir) {    if (!dir) {
982        if (html)
983          printf("<li class='warning'>");
984      printf("    Transfer-Encoding '%s':\n", name);      printf("    Transfer-Encoding '%s':\n", name);
985        if (html)
986          printf("</li>\n");
987      lookup("unknowntransenc");      lookup("unknowntransenc");
988    }    }
989  }  }
# Line 986  void header_set_cookie(const char *s) Line 1047  void header_set_cookie(const char *s)
1047      else      else
1048        s2 = s;        s2 = s;
1049    
1050      if (strncmp(s2, "expires=", 8) == 0) {      if (strncasecmp(s2, "expires=", 8) == 0) {
1051        s2 += 8;        s2 += 8;
1052        r = regexec(&re_cookie_expires, s2, 20, pmatch, 0);        r = regexec(&re_cookie_expires, s2, 20, pmatch, 0);
1053        if (r == 0) {        if (r == 0) {
# Line 1009  void header_set_cookie(const char *s) Line 1070  void header_set_cookie(const char *s)
1070          lookup("cookiebaddate");          lookup("cookiebaddate");
1071          ok = false;          ok = false;
1072        }        }
1073      } else if (strncmp(s2, "domain=", 7) == 0) {      } else if (strncasecmp(s2, "domain=", 7) == 0) {
1074      } else if (strncmp(s2, "path=", 5) == 0) {      } else if (strncasecmp(s2, "path=", 5) == 0) {
1075        if (s2[5] != '/') {        if (s2[5] != '/') {
1076          lookup("cookiebadpath");          lookup("cookiebadpath");
1077          ok = false;          ok = false;
1078        }        }
1079      } else if (strcmp(s, "secure") == 0) {      } else if (strcasecmp(s, "secure") == 0) {
1080      } else {      } else {
1081          if (html)
1082            printf("<li class='warning'>");
1083        printf("    Set-Cookie field '%s':\n", s2);        printf("    Set-Cookie field '%s':\n", s2);
1084          if (html)
1085            printf("</li>\n");
1086        lookup("cookieunknownfield");        lookup("cookieunknownfield");
1087        ok = false;        ok = false;
1088      }      }
# Line 1044  void die(const char *error) Line 1109  void die(const char *error)
1109    
1110    
1111  /**  /**
  * 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);  
 }  
   
   
 /**  
1112   * Print a string which contains control characters.   * Print a string which contains control characters.
1113   */   */
1114  void print(const char *s, size_t len)  void print(const char *s, size_t len)
1115  {  {
1116    size_t i;    size_t i;
1117    for (i = 0; i != len; i++) {    for (i = 0; i != len; i++) {
1118      if (31 < s[i] && s[i] < 127)      if (html && s[i] == '<')
1119          printf("&lt;");
1120        else if (html && s[i] == '>')
1121          printf("&gt;");
1122        else if (html && s[i] == '&')
1123          printf("&amp;");
1124        else if (31 < s[i] && s[i] < 127)
1125        putchar(s[i]);        putchar(s[i]);
1126      else      else {
1127          if (html)
1128            printf("<span class='cc'>");
1129        printf("[%.2x]", s[i]);        printf("[%.2x]", s[i]);
1130          if (html)
1131            printf("</span>");
1132        }
1133    }    }
1134  }  }
1135    
# Line 1146  struct message_entry { Line 1204  struct message_entry {
1204                        "will be deleted by browsers." },                        "will be deleted by browsers." },
1205    { "cookieunknownfield", "Warning: This is not a standard Set-Cookie "    { "cookieunknownfield", "Warning: This is not a standard Set-Cookie "
1206                            "field." },                            "field." },
1207      { "endofheaders", "End of headers." },
1208    { "futurehttp", "Warning: I only understand HTTP/1.1. Check for a newer "    { "futurehttp", "Warning: I only understand HTTP/1.1. Check for a newer "
1209                    "version of this tool." },                    "version of this tool." },
1210    { "futurelastmod", "Error: The specified Last-Modified date-time is in "    { "futurelastmod", "Error: The specified Last-Modified date-time is in "
1211                       "the future." },                       "the future." },
1212      { "headertoolong", "Warning: Header too long: ignored." },
1213    { "missingcolon", "Error: Headers must be of the form 'Name: value'." },    { "missingcolon", "Error: Headers must be of the form 'Name: value'." },
1214    { "missingcontenttype", "Warning: No Content-Type header was present. The "    { "missingcontenttype", "Warning: No Content-Type header was present. The "
1215                            "client will have to guess the media type or ask "                            "client will have to guess the media type or ask "
# Line 1213  void lookup(const char *key) Line 1273  void lookup(const char *key)
1273    else    else
1274      s = key;      s = key;
1275    
1276    printf("    ");    if (html) {
1277    x = 4;      if (strncmp(s, "Warning:", 8) == 0)
1278    while (*s) {        printf("<li class='warning'>");
1279      spc = strchr(s, ' ');      else if (strncmp(s, "Error:", 6) == 0)
1280      if (!spc)        printf("<li class='error'>");
1281        spc = s + strlen(s);      else if (strncmp(s, "OK", 2) == 0)
1282      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;  
1283      else      else
1284        s = spc;        printf("<li>");
1285        for (; *s; s++) {
1286          if (strncmp(s, "http://", 7) == 0) {
1287            spc = strchr(s, ' ');
1288            printf("<a href='%.*s'>%.*s</a>", spc - s, s, spc - s, s);
1289            s = spc;
1290          }
1291          switch (*s) {
1292            case '<': printf("&lt;"); break;
1293            case '>': printf("&gt;"); break;
1294            case '&': printf("&amp;"); break;
1295            default: printf("%c", *s); break;
1296          }
1297        }
1298        printf("</li>\n");
1299    
1300      } else {
1301        printf("    ");
1302        x = 4;
1303        while (*s) {
1304          spc = strchr(s, ' ');
1305          if (!spc)
1306            spc = s + strlen(s);
1307          if (75 < x + (spc - s)) {
1308            printf("\n    ");
1309            x = 4;
1310          }
1311          x += spc - s + 1;
1312          printf("%.*s ", spc - s, s);
1313          if (*spc)
1314            s = spc + 1;
1315          else
1316            s = spc;
1317        }
1318        printf("\n\n");
1319    }    }
   printf("\n\n");  
1320  }  }
1321    

Legend:
Removed from v.55  
changed lines
  Added in v.56

  ViewVC Help
Powered by ViewVC 1.1.26