1 #include "sysinstall.h"
2 #include <sys/socket.h>
3 #include <netinet/in.h>
11 mediaInitHTTP(Device *dev)
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.
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().
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;
29 s=socket(PF_INET, SOCK_STREAM, 6); /* tcp */
31 msgConfirm("Network error");
35 peer_in=gethostbyname(variable_get(VAR_HTTP_HOST));
36 if (peer_in == NULL) {
37 msgConfirm("%s",hstrerror(h_errno));
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);
46 rv=connect(s,(struct sockaddr *)&peer,sizeof(peer));
48 msgConfirm("Couldn't connect to proxy %s:%s",
49 variable_get(VAR_HTTP_HOST),variable_get(VAR_HTTP_PORT));
53 sprintf(req,"GET / HTTP/1.0\r\n\r\n");
54 write(s,req,strlen(req));
56 * scan the headers of the response
57 * this is extremely quick'n dirty
63 variable_set2(VAR_HTTP_FTP_MODE,"",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);
71 variable_set2(VAR_HTTP_FTP_MODE,"",0);
74 /* ignore other headers */
75 /* check for "\015\012" at beginning of line, i.e. end of headers */
94 mediaGetHTTP(Device *dev, char *file, Boolean probe)
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;
103 s=socket(PF_INET, SOCK_STREAM, 6); /* tcp */
105 msgConfirm("Network error");
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);
115 rv=connect(s,(struct sockaddr *)&peer,sizeof(peer));
117 msgConfirm("Couldn't connect to proxy %s:%s",
118 variable_get(VAR_HTTP_HOST),variable_get(VAR_FTP_PORT));
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));
127 msgDebug("sending http request: %s",req);
129 write(s,req,strlen(req));
132 * scan the headers of the response
133 * this is extremely quick'n dirty
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)) {
147 } else if (rv >= 500) {
148 msgConfirm("Server error %s when sending %s, you could try an other server",buf, req);
150 } else if (rv == 404) {
151 msgConfirm("%s was not found, maybe directory or release-version are wrong?",req);
153 } else if (rv >= 400) {
154 msgConfirm("Client error %s, you could try an other server",buf);
156 } else if (rv >= 300) {
157 msgConfirm("Error %s,",buf);
159 } else if (rv != 200) {
160 msgConfirm("Error %s when sending %s, you could try an other server",buf, req);
164 /* ignore other headers */
165 /* check for "\015\012" at beginning of line, i.e. end of headers */