File: eperl_http.c

package info (click to toggle)
eperl 2.2.15-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 648 kB
  • sloc: ansic: 1,692; perl: 252; makefile: 139; sh: 10
file content (248 lines) | stat: -rw-r--r-- 7,485 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
/* SPDX-License-Identifier: Artistic-1.0-Perl OR GPL-2.0-only
**  Copyright (c) 1996,1997,1998 Ralf S. Engelschall <rse@engelschall.com>
*/

#include "eperl.h"
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
#define min(a, b) ((a) < (b) ? (a) : (b))


/*
**  check if the line is a valid HTTP header line
*/
static bool HTTP_IsHeaderLine(const char *beg, size_t len)
{
    char *colon = memchr(beg, ':', len);
    if (!colon)
        return false;
    size_t validlen = strspn(beg, "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz01234567890-_");
    return (size_t)(colon - beg) == validlen;
}

/*
**  check if there a particular HTTP headerline exists
*/
static bool HTTP_HeaderLineExists(const char *cpBuf, const char *name)
{
    size_t n = strlen(name);
    const char *cp2 = NULL, *cp2a;
    if ((cp2a = strstr(cpBuf, "\n\n")) != NULL)
        cp2 = cp2a;
    if ((cp2a = strstr(cpBuf, "\r\n\r\n")) != NULL && (cp2 == NULL || cp2a < cp2))
        cp2 = cp2a;
    if (cp2 != NULL) {
        for (const char *cp1 = cpBuf; cp1 < cp2-1; ) {
            const char *cp3 = strchr(cp1, '\n');
            if ((size_t)(cp3-cp1) > n+1 && HTTP_IsHeaderLine(cp1, cp3-cp1))
                if (strncasecmp(cp1, name, n) == 0)
                    return true;
            cp1 = cp3+1;
        }
        return false;
    }
    return false;
}

/*
**  print a standard HTTP reponse of header lines
*/
size_t HTTP_PrintResponseHeaders(const char *cpBuf)
{
    size_t skip = 0;
    const char *statend;
    if (  (!strncmp(cpBuf, "HTTP/1.0 ", strlen("HTTP/1.0 "))
        || !strncmp(cpBuf, "HTTP/1.1 ", strlen("HTTP/1.1 ")))
        && (cpBuf[ 9] >= '1' && cpBuf[ 9] <= '5')
        && (cpBuf[10] >= '0' && cpBuf[10] <= '9')
        && (cpBuf[11] >= '0' && cpBuf[11] <= '9')
        && (cpBuf[12] == ' ')
        && ((statend = strchr(cpBuf + 12, '\n')) != NULL)) {
        /* found HTTP status code */
        printf("%.*s\r\n", (int)(statend - cpBuf - (*(statend - 1) == '\r')), cpBuf);
        skip = statend - cpBuf + 1;
    } else {
        /* no HTTP status code */
        printf("%s 200 OK\r\n", getenv("SERVER_PROTOCOL") ?: "HTTP/1.0");
    }

    if (!HTTP_HeaderLineExists(cpBuf, "Server"))
        printf("Server: %s ePerl/%s Perl/%s\r\n", getenv("SERVER_SOFTWARE") ?: "unknown-server/0.0", EPERL_VERSION_SHORT, AC_perl_vers);

    if (!HTTP_HeaderLineExists(cpBuf, "Date"))
        printf("Date: %.24s\r\n", ctime(&(time_t){time(NULL)}));

    if (!HTTP_HeaderLineExists(cpBuf, "Connection"))
        printf("Connection: close\r\n");

    return skip;
}

/*
**  check if there is a valid HTTP header
*/
bool HTTP_HeadersExists(const char *cpBuf)
{
    const char *cp2 = NULL, *cp2a;
    if ((cp2a = strstr(cpBuf, "\n\n")) != NULL)
        cp2 = cp2a;
    if ((cp2a = strstr(cpBuf, "\r\n\r\n")) != NULL && (cp2 == NULL || cp2a < cp2))
        cp2 = cp2a;
    if (cp2 != NULL) {
        for (const char *cp1 = cpBuf; cp1 < cp2-1; ) {
            const char *cp3 = strchr(cp1, '\n');
            if (!HTTP_IsHeaderLine(cp1, cp3 - cp1))
                return false;
            cp1 = cp3+1;
        }
        return true;
    }
    return false;
}


/*
**  extracts the host name from an url
*/
struct slice { char *data; size_t len; };
static struct slice HTTP_HostOfURL(char *url)
{
    char *cps = strstr(url, "//") + 2, *cpe;
    for (cpe = cps; *cpe != '/' && *cpe != ':' && *cpe != '\0'; cpe++)
        ;
    return (struct slice){ .data = cps, .len = cpe-cps };
}

/*
**  extracts the port from an url
*/
static struct slice HTTP_PortOfURL(char *url)
{
    char *cps = strstr(url, "//") + 2, *cpe;
    for ( ; *cps != '/' && *cps != ':' && *cps != '\0'; cps++)
        ;
    if (*cps == ':') {
        cps++;
        for (cpe = cps; *cpe != '/' && *cpe != '\0'; cpe++)
            ;
        return (struct slice){ .data = cps, .len = cpe-cps };
    }
    else
        return (struct slice){ .data = "80", .len = 2 };
}

/*
**  extracts path from an url
*/
static const char *HTTP_FileOfURL(const char *url)
{
    return (strchr(strstr(url, "//") + 2, '/') ?: "/") + 1;
}

/*
**  open an URL as a file descriptor
*/
FILE *HTTP_openURLasFP(const char *url_arg)
{
    struct addrinfo *ai;
    char buf[8 * 1024];
    struct slice host, port;
    const char *file;
    char *cp;
    int s;

    char *url = (char *)url_arg;
    bool url_rw = false;
redirected:
    host = HTTP_HostOfURL(url);
    port = HTTP_PortOfURL(url);
    file = HTTP_FileOfURL(url);

    /* get the host name */
    if (host.data[host.len]) {
        if (!url_rw) {
            host.len = min(sizeof(buf) - 128, host.len);
            host.data = memcpy(buf, host.data, host.len);
        }
        host.data[host.len] = '\0';
    }
    if (port.data[port.len]) {
        if (!url_rw) {
            port.len = min(126, port.len);
            port.data = memcpy(buf + sizeof(buf) - 127, port.data, port.len);
        }
        port.data[port.len] = '\0';
    }
    if (getaddrinfo(host.data, port.data, NULL, &ai) != 0)
        return NULL;
    if ((s = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP)) == -1) {
        freeaddrinfo(ai);
        return NULL;
    }
    if (connect(s, ai->ai_addr, ai->ai_addrlen) == -1) {
        freeaddrinfo(ai);
        close(s);
        return NULL;
    }
    freeaddrinfo(ai);

    /* send HTTP/1.0 request */
    FILE *fp = fdopen(s, "r+");
    fprintf(fp,
        "GET /%s HTTP/1.0\r\n"
        "Host: %s:%s\r\n"
        "User-Agent: ePerl/%s\r\n"
        "\r\n",
        file, host.data, port.data, EPERL_VERSION_SHORT);
    fflush(fp);

    /* read the HTTP response line and check for 200 OK response */
    if (fgets(buf, sizeof(buf), fp) == NULL) {
    fclose:
        fclose(fp);
        return NULL;
    }
    if (strncmp(buf, "HTTP/1.", 7) || (buf[7] != '0' && buf[7] != '1'))
        goto fclose;
    for (cp = buf+8; *cp == ' ' || *cp == '\t'; cp++)
        ;
    if (strncmp(cp, "200", 3 /* OK */) != 0) {
        if (strncmp(cp, "301", 3 /* MOVED PERMANENTLY */) != 0 ||
            strncmp(cp, "302", 3 /* MOVED TEMPORARILY */) != 0   ) {
            /* we try to determine the new URL from
               the HTTP header 'Location' and restart from
               the beginning if an URL is found */
            while (fgets(buf, sizeof(buf), fp) != NULL) {
                if ((*buf == '\n' && *(buf+1) == '\0') ||
                    (*buf == '\n' && *(buf+1) == '\r' && *(buf+2) == '\0') ||
                    (*buf == '\r' && *(buf+1) == '\n' && *(buf+2) == '\0'))
                    break;
                if (strncasecmp(buf, "Location:", 9) == 0) {
                    char *newurl = buf + 9;
                    newurl += strspn(newurl, " \t");
                    newurl[strcspn(newurl, " \t\r\n")] = '\0';
                    fclose(fp);
                    if (strncmp(newurl, "http://", 7))
                        return NULL;
                    url    = newurl;
                    url_rw = true;
                    goto redirected;
                }
            }
            return NULL;
        }
        goto fclose;
    }

    /* now read until a blank line, i.e. skip HTTP headers */
    while (fgets(buf, sizeof(buf), fp) != NULL) {
        if ((*buf == '\n' && *(buf+1) == '\0') ||
            (*buf == '\n' && *(buf+1) == '\r' && *(buf+2) == '\0') ||
            (*buf == '\r' && *(buf+1) == '\n' && *(buf+2) == '\0'))
            break;
    }

    /* return the (still open) FILE pointer */
    return fp;
}