Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100644 108 lines (90 sloc) 2.506 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
5 char id_http[] = "$Id: http.c,v 2.11 2003/10/17 17:41:23 steve Exp $";
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)
38 || !netputline(sb, "", NULL, NULL))
39 {
40 /* error message already given */
9f1c6f0 @mbert Import Elvis 2.2_0 (written by Steve Kirkendall)
authored
41 return ElvFalse;
8d1ac0c @mbert Import Elvis 2.1 (written by Steve Kirkendall)
authored
42 }
43
44 /* Success, so far. Now ready to begin reading data. */
45 msg(MSG_STATUS, "receiving...");
46
47 /* Fill the receive buffer. */
48 if (!netread(sb))
9f1c6f0 @mbert Import Elvis 2.2_0 (written by Steve Kirkendall)
authored
49 return ElvFalse;
8d1ac0c @mbert Import Elvis 2.1 (written by Steve Kirkendall)
authored
50
51 /* If the response starts with "HTTP", then assume it is in MIME
52 * format. Skip the header, and watch for a "Content-Length:" line.
53 */
54 if (netbytes(sb) >= 4 && !strncmp(netbuffer(sb), "HTTP", 4))
55 {
56 while ((line = netgetline(sb)) != NULL && *line)
57 {
58 /* Convert the header label to lowercase */
59 for (p = line; *p && *p != ':'; p++)
60 {
9f1c6f0 @mbert Import Elvis 2.2_0 (written by Steve Kirkendall)
authored
61 *p = elvtolower(*p);
8d1ac0c @mbert Import Elvis 2.1 (written by Steve Kirkendall)
authored
62 }
63
64 /* If "Content-Length:" then allow url.c to display
65 * the size.
66 */
67 if (!strncmp(line, "content-length: ", 16))
68 {
69 urlbytes(atol(&line[16]));
70 }
71 }
72 }
73
9f1c6f0 @mbert Import Elvis 2.2_0 (written by Steve Kirkendall)
authored
74 return ElvTrue;
8d1ac0c @mbert Import Elvis 2.1 (written by Steve Kirkendall)
authored
75 }
76
77
78 /* Read some data from the remote server */
79 int httpread(buf, nbytes)
80 CHAR *buf;
81 int nbytes;
82 {
83 /* try to fill sb's buffer */
84 if (sb->left > 0 && !netread(sb))
85 return -1;
86
87 /* never read more data than there is in sb's buffer */
88 if (netbytes(sb) < nbytes)
89 nbytes = netbytes(sb);
90
91 /* copy bytes from sb's buffer to buf */
92 if (nbytes > 0)
93 {
94 memcpy(buf, netbuffer(sb), nbytes);
95 netconsume(sb, nbytes);
96 }
97 return nbytes;
98 }
99
100 /* close the socket */
9f1c6f0 @mbert Import Elvis 2.2_0 (written by Steve Kirkendall)
authored
101 void httpclose()
8d1ac0c @mbert Import Elvis 2.1 (written by Steve Kirkendall)
authored
102 {
103 netdisconnect(sb);
104 }
105
106
107 #endif /* PROTOCOL_HTTP */
Something went wrong with that request. Please try again.