Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100644 109 lines (91 sloc) 2.56 kB
8d1ac0c @mbert Import Elvis 2.1 (written by Steve Kirkendall)
authored
1 /* http.c */
2
3 #include "elvis.h"
9f1c6f0 @mbert Import Elvis 2.2_0 (written by Steve Kirkendall)
authored
4 #ifdef FEATURE_RCSID
2fe6e17 @mbert Import Elvis 2.2_1 (written by Steve Kirkendall)
authored
5 char id_http[] = "$Id: http.c,v 2.12 2003/12/28 20:29:02 steve Exp $";
9f1c6f0 @mbert Import Elvis 2.2_0 (written by Steve Kirkendall)
authored
6 #endif
8d1ac0c @mbert Import Elvis 2.1 (written by Steve Kirkendall)
authored
7 #ifdef PROTOCOL_HTTP
8
9 static sockbuf_t *sb;
10
9f1c6f0 @mbert Import Elvis 2.2_0 (written by Steve Kirkendall)
authored
11 /* Open an HTTP resource for reading. Returns ElvTrue if successful, or False
8d1ac0c @mbert Import Elvis 2.1 (written by Steve Kirkendall)
authored
12 * for error (after giving an error message).
13 */
9f1c6f0 @mbert Import Elvis 2.2_0 (written by Steve Kirkendall)
authored
14 ELVBOOL httpopen(site_port, resource)
8d1ac0c @mbert Import Elvis 2.1 (written by Steve Kirkendall)
authored
15 char *site_port; /* host name and optional port number */
16 char *resource; /* name of file at remote host */
17 {
18 char *line, *p;
19
20 /* Open a connection to the server */
21 sb = netconnect(site_port, 80);
22 if (!sb)
23 {
24 /* error message already given */
9f1c6f0 @mbert Import Elvis 2.2_0 (written by Steve Kirkendall)
authored
25 return ElvFalse;
8d1ac0c @mbert Import Elvis 2.1 (written by Steve Kirkendall)
authored
26 }
27
28 /* Send the "GET" request to the HTTP server. Note that netputline()
29 * will add a second CRLF pair after the "HTTP/1.0" string. This is
30 * important, because when an HTTP server sees "HTTP/1.0" it expects
9f1c6f0 @mbert Import Elvis 2.2_0 (written by Steve Kirkendall)
authored
31 * supplemental information to appear on following lines, up to the
8d1ac0c @mbert Import Elvis 2.1 (written by Steve Kirkendall)
authored
32 * next blank line. It won't process the request until it sees that
33 * blank line.
34 */
35 msg(MSG_STATUS, "[s]requesting $1", resource);
36 if (!netputline(sb, "GET", resource, "HTTP/1.0")
37 || !netputline(sb, "Host:", site_port, NULL)
2fe6e17 @mbert Import Elvis 2.2_1 (written by Steve Kirkendall)
authored
38 || !netputline(sb, "User-Agent:", "Elvis", VERSION)
8d1ac0c @mbert Import Elvis 2.1 (written by Steve Kirkendall)
authored
39 || !netputline(sb, "", NULL, NULL))
40 {
41 /* error message already given */
9f1c6f0 @mbert Import Elvis 2.2_0 (written by Steve Kirkendall)
authored
42 return ElvFalse;
8d1ac0c @mbert Import Elvis 2.1 (written by Steve Kirkendall)
authored
43 }
44
45 /* Success, so far. Now ready to begin reading data. */
46 msg(MSG_STATUS, "receiving...");
47
48 /* Fill the receive buffer. */
49 if (!netread(sb))
9f1c6f0 @mbert Import Elvis 2.2_0 (written by Steve Kirkendall)
authored
50 return ElvFalse;
8d1ac0c @mbert Import Elvis 2.1 (written by Steve Kirkendall)
authored
51
52 /* If the response starts with "HTTP", then assume it is in MIME
53 * format. Skip the header, and watch for a "Content-Length:" line.
54 */
55 if (netbytes(sb) >= 4 && !strncmp(netbuffer(sb), "HTTP", 4))
56 {
57 while ((line = netgetline(sb)) != NULL && *line)
58 {
59 /* Convert the header label to lowercase */
60 for (p = line; *p && *p != ':'; p++)
61 {
9f1c6f0 @mbert Import Elvis 2.2_0 (written by Steve Kirkendall)
authored
62 *p = elvtolower(*p);
8d1ac0c @mbert Import Elvis 2.1 (written by Steve Kirkendall)
authored
63 }
64
65 /* If "Content-Length:" then allow url.c to display
66 * the size.
67 */
68 if (!strncmp(line, "content-length: ", 16))
69 {
70 urlbytes(atol(&line[16]));
71 }
72 }
73 }
74
9f1c6f0 @mbert Import Elvis 2.2_0 (written by Steve Kirkendall)
authored
75 return ElvTrue;
8d1ac0c @mbert Import Elvis 2.1 (written by Steve Kirkendall)
authored
76 }
77
78
79 /* Read some data from the remote server */
80 int httpread(buf, nbytes)
81 CHAR *buf;
82 int nbytes;
83 {
84 /* try to fill sb's buffer */
85 if (sb->left > 0 && !netread(sb))
86 return -1;
87
88 /* never read more data than there is in sb's buffer */
89 if (netbytes(sb) < nbytes)
90 nbytes = netbytes(sb);
91
92 /* copy bytes from sb's buffer to buf */
93 if (nbytes > 0)
94 {
95 memcpy(buf, netbuffer(sb), nbytes);
96 netconsume(sb, nbytes);
97 }
98 return nbytes;
99 }
100
101 /* close the socket */
9f1c6f0 @mbert Import Elvis 2.2_0 (written by Steve Kirkendall)
authored
102 void httpclose()
8d1ac0c @mbert Import Elvis 2.1 (written by Steve Kirkendall)
authored
103 {
104 netdisconnect(sb);
105 }
106
107
108 #endif /* PROTOCOL_HTTP */
Something went wrong with that request. Please try again.