]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - usr.sbin/sysinstall/http.c
This commit was generated by cvs2svn to compensate for changes in r61521,
[FreeBSD/FreeBSD.git] / usr.sbin / sysinstall / http.c
1 #include "sysinstall.h"
2 #include <sys/socket.h>
3 #include <netinet/in.h>
4 #include <arpa/inet.h>
5 #include <sys/param.h>
6 #include <netdb.h>
7
8 int HttpPort;
9
10 Boolean
11 mediaInitHTTP(Device *dev)
12 {
13 /* 
14  * Some proxies fetch files with certain extensions in "ascii mode" instead
15  * of "binary mode" for FTP. The FTP server then translates all LF to CRLF.
16  *
17  * You can force Squid to use binary mode by appending ";type=i" to the URL,
18  * which is what I do here. For other proxies, the LF->CRLF substitution
19  * is reverted in distExtract().
20  */
21
22     extern int h_errno;
23     int rv,s;
24     bool el;                /* end of header line */
25     char *cp, buf[PATH_MAX], req[BUFSIZ];
26     struct sockaddr_in peer;
27     struct hostent *peer_in;
28
29     s=socket(PF_INET, SOCK_STREAM, 6);    /* tcp */
30     if (s == -1) {
31         msgConfirm("Network error");
32         return FALSE;
33     }
34
35     peer_in=gethostbyname(variable_get(VAR_HTTP_HOST));
36     if (peer_in == NULL) {
37         msgConfirm("%s",hstrerror(h_errno));
38         return FALSE;
39     }
40
41     peer.sin_len=peer_in->h_length;
42     peer.sin_family=peer_in->h_addrtype;
43     peer.sin_port=htons((u_short) HttpPort);
44     bcopy(peer_in->h_addr_list[0], &peer.sin_addr, peer_in->h_length);
45
46     rv=connect(s,(struct sockaddr *)&peer,sizeof(peer));
47     if (rv == -1) {
48         msgConfirm("Couldn't connect to proxy %s:%s",
49                     variable_get(VAR_HTTP_HOST),variable_get(VAR_HTTP_PORT));
50         return FALSE;
51     }
52
53     sprintf(req,"GET / HTTP/1.0\r\n\r\n");
54     write(s,req,strlen(req));
55 /*
56  *  scan the headers of the response
57  *  this is extremely quick'n dirty
58  *
59  */
60     cp=buf;
61     el=FALSE;
62     rv=read(s,cp,1);
63     variable_set2(VAR_HTTP_FTP_MODE,"",0);
64     while (rv>0) {
65         if ((*cp == '\012') && el) { 
66             /* reached end of a header line */
67             if (!strncmp(buf,"Server: ",8)) {
68                 if (!strncmp(buf,"Server: Squid",13)) {
69                     variable_set2(VAR_HTTP_FTP_MODE,";type=i",0);
70                 } else {
71                     variable_set2(VAR_HTTP_FTP_MODE,"",0);
72                 }
73             }
74             /* ignore other headers */
75             /* check for "\015\012" at beginning of line, i.e. end of headers */
76             if ((cp-buf) == 1)
77                 break;
78             cp=buf;
79             rv=read(s,cp,1);
80         } else {
81             el=FALSE;
82             if (*cp == '\015')
83                 el=TRUE;
84             cp++;
85             rv=read(s,cp,1);
86         }
87     }
88     close(s);
89     return TRUE;
90
91
92
93 FILE *
94 mediaGetHTTP(Device *dev, char *file, Boolean probe)
95 {
96     FILE *fp;
97     int rv,s;
98     bool el;                    /* end of header line */
99     char *cp, buf[PATH_MAX], req[BUFSIZ];
100     struct sockaddr_in peer;
101     struct hostent *peer_in;
102
103     s=socket(PF_INET, SOCK_STREAM, 6);    /* tcp */
104     if (s == -1) {
105         msgConfirm("Network error");
106         return NULL;
107     }
108       
109     peer_in=gethostbyname(variable_get(VAR_HTTP_HOST));
110     peer.sin_len=peer_in->h_length;
111     peer.sin_family=peer_in->h_addrtype;
112     peer.sin_port=htons((u_short) HttpPort);
113     bcopy(peer_in->h_addr_list[0], &peer.sin_addr, peer_in->h_length);
114
115     rv=connect(s,(struct sockaddr *)&peer,sizeof(peer));
116     if (rv == -1) {
117         msgConfirm("Couldn't connect to proxy %s:%s",
118                     variable_get(VAR_HTTP_HOST),variable_get(VAR_FTP_PORT));
119         return NULL;
120     }
121                                                    
122     sprintf(req,"GET %s/%s/%s%s HTTP/1.0\r\n\r\n",
123             variable_get(VAR_FTP_PATH), variable_get(VAR_RELNAME),
124             file, variable_get(VAR_HTTP_FTP_MODE));
125
126     if (isDebug()) {
127         msgDebug("sending http request: %s",req);
128     }
129     write(s,req,strlen(req));
130
131 /*
132  *  scan the headers of the response
133  *  this is extremely quick'n dirty
134  *
135  */
136     cp=buf;
137     el=FALSE;
138     rv=read(s,cp,1);
139     while (rv>0) {
140         if ((*cp == '\012') && el) {
141             /* reached end of a header line */
142             if (!strncmp(buf,"HTTP",4)) {
143                 rv=strtol((char *)(buf+9),0,0);
144                 *(cp-1)='\0';           /* chop the CRLF off */
145                 if (probe && (rv != 200)) {
146                     return NULL;
147                 } else if (rv >= 500) {
148                     msgConfirm("Server error %s when sending %s, you could try an other server",buf, req);
149                     return NULL;
150                 } else if (rv == 404) {
151                     msgConfirm("%s was not found, maybe directory or release-version are wrong?",req);
152                     return NULL;
153                 } else if (rv >= 400) {
154                     msgConfirm("Client error %s, you could try an other server",buf);
155                     return NULL;
156                 } else if (rv >= 300) {
157                     msgConfirm("Error %s,",buf);
158                     return NULL;
159                 } else if (rv != 200) {
160                     msgConfirm("Error %s when sending %s, you could try an other server",buf, req);
161                     return NULL;
162                 }
163             }
164             /* ignore other headers */
165             /* check for "\015\012" at beginning of line, i.e. end of headers */
166             if ((cp-buf) == 1) 
167                 break;
168             cp=buf;
169             rv=read(s,cp,1);
170         } else {
171             el=FALSE;
172             if (*cp == '\015')
173                 el=TRUE;
174             cp++;
175             rv=read(s,cp,1);
176         }
177     }
178     fp=fdopen(s,"r");
179     return fp;
180 }