eris

Very small inetd http server
git clone https://git.woozle.org/neale/eris.git

Neale Pickett  ·  2014-06-23

strings.c

  1#include <stdio.h>
  2#include <ctype.h>
  3#include "strings.h"
  4
  5int
  6endswith(char *haystack, char *needle)
  7{
  8    char *h, *n;
  9
 10    for (h = haystack; *h; h++);
 11    for (n = needle;   *n; n++);
 12
 13    if (h - haystack < n - needle) {
 14        return 0;
 15    }
 16
 17    while (n >= needle) {
 18        if (*(n--) != *(h--)) {
 19            return 0;
 20        }
 21    }
 22
 23    return 1;
 24}
 25
 26/** Replace whitespace with underscores for logging */
 27void
 28sanitize(char *s)
 29{
 30    if (!s) {
 31        return;
 32    }
 33    for (; *s; s += 1) {
 34        if (isspace(*s)) {
 35            *s = '_';
 36        }
 37    }
 38}
 39
 40/** Parse a header out of a line.
 41 *
 42 * This capitalizes the header name, and strips trailing [\r\n]
 43 * Returns the length of the line (after stripping)
 44 */
 45size_t
 46extract_header_field(char *buf, char **val, int cgi)
 47{
 48    size_t len;
 49
 50    *val = NULL;
 51
 52    for (len = 0; buf[len]; len += 1) {
 53        if (! *val) {
 54            if (buf[len] == '\n') {
 55                /* Blank line or incorrectly-formatted header */
 56                break;
 57            } else if (buf[len] == ':') {
 58                buf[len] = 0;
 59                for (*val = &(buf[len+1]); **val == ' '; *val += 1);
 60            } else if (cgi) {
 61                switch (buf[len]) {
 62                    case 'a'...'z':
 63                        buf[len] ^= ' ';
 64                        break;
 65                    case 'A'...'Z':
 66                    case '0'...'9':
 67                    case '\r':
 68                    case '\n':
 69                        break;
 70                    default:
 71                        buf[len] = '_';
 72                        break;
 73                }
 74            }
 75        }
 76    }
 77
 78    for (; (len > 0) && ((buf[len-1] == '\n') || (buf[len-1] == '\r')); len -= 1);
 79    buf[len] = 0;
 80
 81    return len;
 82}
 83
 84
 85int
 86fromhex(int c)
 87{
 88    if (c >= '0' && c <= '9')
 89        return c - '0';
 90    else {
 91        c |= ' ';
 92        if (c >= 'a' && c <= 'f')
 93            return c - 'a' + 10;
 94    }
 95    return -1;
 96}
 97
 98void
 99html_esc(FILE *f, char *s)
100{
101    for (; *s; s += 1) {
102        switch (*s) {
103            case '<':
104                fprintf(f, "&lt;");
105                break;
106            case '>':
107                fprintf(f, "&gt;");
108                break;
109            case '&':
110                fprintf(f, "&amp;");
111                break;
112            default:
113                fputc(*s, f);
114                break;
115        }
116    }
117}
118
119void
120url_esc(FILE *f, char *s)
121{
122    for (; *s; s += 1) {
123        if ((*s == '%') || (*s == 127) || (*s < 31)) {
124            fprintf(f, "%%%02x", *s);
125        } else {
126            fputc(*s, f);
127        }
128    }
129}