Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

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