|
Lines 11-48
Link Here
|
| 11 |
mediaInitHTTP(Device *dev) |
11 |
mediaInitHTTP(Device *dev) |
| 12 |
{ |
12 |
{ |
| 13 |
/* |
13 |
/* |
| 14 |
* Some proxies think that files with the extension ".ai" are postscript |
14 |
* Some proxies fetch files with certain extensions in "ascii mode" instead |
| 15 |
* files and use "ascii mode" instead of "binary mode" for ftp. |
15 |
* of "binary mode" for FTP. The FTP server then translates all LF to CRLF. |
| 16 |
* The FTP server then translates all LF to CRLF. |
|
|
| 17 |
* I don't know how to handle this elegantly... |
| 18 |
* Squid uses ascii mode, ftpget uses binary mode and both tell us: |
| 19 |
* "Content-Type: application/postscript" |
| 20 |
* |
| 21 |
* Probably the safest way would be to get the file, look at its checksum |
| 22 |
* and, if it doesn't match, replace all CRLF by LF and check again. |
| 23 |
* |
16 |
* |
| 24 |
* You can force Squid to use binary mode by appending ";type=i" to the URL, |
17 |
* You can force Squid to use binary mode by appending ";type=i" to the URL, |
| 25 |
* which is what I do here. |
18 |
* which is what I do here. For other proxies, the LF->CRLF substitution |
| 26 |
* |
19 |
* is reverted in distExtract(). |
| 27 |
*/ |
20 |
*/ |
| 28 |
|
21 |
|
| 29 |
extern int h_errno; |
22 |
extern int h_errno; |
| 30 |
int rv,s; |
23 |
int rv,s; |
| 31 |
bool el; /* end of header line */ |
24 |
bool el; /* end of header line */ |
| 32 |
char *cp, buf[PATH_MAX], req[1000]; |
25 |
char *cp, buf[PATH_MAX], req[BUFSIZ]; |
| 33 |
struct sockaddr_in peer; |
26 |
struct sockaddr_in peer; |
| 34 |
struct hostent *peer_in; |
27 |
struct hostent *peer_in; |
| 35 |
|
28 |
|
| 36 |
s=socket(PF_INET, SOCK_STREAM, 6); /* tcp */ |
29 |
s=socket(PF_INET, SOCK_STREAM, 6); /* tcp */ |
| 37 |
if (s == -1) { |
30 |
if (s == -1) { |
| 38 |
msgConfirm("Network error"); |
31 |
msgConfirm("Network error"); |
| 39 |
return FALSE; |
32 |
return FALSE; |
| 40 |
} |
33 |
} |
| 41 |
|
34 |
|
| 42 |
peer_in=gethostbyname(variable_get(VAR_HTTP_HOST)); |
35 |
peer_in=gethostbyname(variable_get(VAR_HTTP_HOST)); |
| 43 |
if (peer_in == NULL) { |
36 |
if (peer_in == NULL) { |
| 44 |
msgConfirm("%s",hstrerror(h_errno)); |
37 |
msgConfirm("%s",hstrerror(h_errno)); |
| 45 |
return FALSE; |
38 |
return FALSE; |
| 46 |
} |
39 |
} |
| 47 |
|
40 |
|
| 48 |
peer.sin_len=peer_in->h_length; |
41 |
peer.sin_len=peer_in->h_length; |
|
Lines 52-60
Link Here
|
| 52 |
|
45 |
|
| 53 |
rv=connect(s,(struct sockaddr *)&peer,sizeof(peer)); |
46 |
rv=connect(s,(struct sockaddr *)&peer,sizeof(peer)); |
| 54 |
if (rv == -1) { |
47 |
if (rv == -1) { |
| 55 |
msgConfirm("Couldn't connect to proxy %s:%s", |
48 |
msgConfirm("Couldn't connect to proxy %s:%s", |
| 56 |
variable_get(VAR_HTTP_HOST),variable_get(VAR_FTP_PORT)); |
49 |
variable_get(VAR_HTTP_HOST),variable_get(VAR_HTTP_PORT)); |
| 57 |
return FALSE; |
50 |
return FALSE; |
| 58 |
} |
51 |
} |
| 59 |
|
52 |
|
| 60 |
sprintf(req,"GET / HTTP/1.0\r\n\r\n"); |
53 |
sprintf(req,"GET / HTTP/1.0\r\n\r\n"); |
|
Lines 69-96
Link Here
|
| 69 |
rv=read(s,cp,1); |
62 |
rv=read(s,cp,1); |
| 70 |
variable_set2(VAR_HTTP_FTP_MODE,"",0); |
63 |
variable_set2(VAR_HTTP_FTP_MODE,"",0); |
| 71 |
while (rv>0) { |
64 |
while (rv>0) { |
| 72 |
if ((*cp == '\012') && el) { |
65 |
if ((*cp == '\012') && el) { |
| 73 |
/* reached end of a header line */ |
66 |
/* reached end of a header line */ |
| 74 |
if (!strncmp(buf,"Server: ",8)) { |
67 |
if (!strncmp(buf,"Server: ",8)) { |
| 75 |
if (!strncmp(buf,"Server: Squid",13)) { |
68 |
if (!strncmp(buf,"Server: Squid",13)) { |
| 76 |
variable_set2(VAR_HTTP_FTP_MODE,";type=i",1); |
69 |
variable_set2(VAR_HTTP_FTP_MODE,";type=i",0); |
| 77 |
} else { |
70 |
} else { |
| 78 |
variable_set2(VAR_HTTP_FTP_MODE,"",1); |
71 |
variable_set2(VAR_HTTP_FTP_MODE,"",0); |
| 79 |
} |
72 |
} |
| 80 |
} |
73 |
} |
| 81 |
/* ignore other headers */ |
74 |
/* ignore other headers */ |
| 82 |
/* check for "\015\012" at beginning of line, i.e. end of headers */ |
75 |
/* check for "\015\012" at beginning of line, i.e. end of headers */ |
| 83 |
if ((cp-buf) == 1) |
76 |
if ((cp-buf) == 1) |
| 84 |
break; |
77 |
break; |
| 85 |
cp=buf; |
78 |
cp=buf; |
| 86 |
rv=read(s,cp,1); |
79 |
rv=read(s,cp,1); |
| 87 |
} else { |
80 |
} else { |
| 88 |
el=FALSE; |
81 |
el=FALSE; |
| 89 |
if (*cp == '\015') |
82 |
if (*cp == '\015') |
| 90 |
el=TRUE; |
83 |
el=TRUE; |
| 91 |
cp++; |
84 |
cp++; |
| 92 |
rv=read(s,cp,1); |
85 |
rv=read(s,cp,1); |
| 93 |
} |
86 |
} |
| 94 |
} |
87 |
} |
| 95 |
close(s); |
88 |
close(s); |
| 96 |
return TRUE; |
89 |
return TRUE; |
|
Lines 103-116
Link Here
|
| 103 |
FILE *fp; |
96 |
FILE *fp; |
| 104 |
int rv,s; |
97 |
int rv,s; |
| 105 |
bool el; /* end of header line */ |
98 |
bool el; /* end of header line */ |
| 106 |
char *cp, buf[PATH_MAX], req[1000]; |
99 |
char *cp, buf[PATH_MAX], req[BUFSIZ]; |
| 107 |
struct sockaddr_in peer; |
100 |
struct sockaddr_in peer; |
| 108 |
struct hostent *peer_in; |
101 |
struct hostent *peer_in; |
| 109 |
|
102 |
|
| 110 |
s=socket(PF_INET, SOCK_STREAM, 6); /* tcp */ |
103 |
s=socket(PF_INET, SOCK_STREAM, 6); /* tcp */ |
| 111 |
if (s == -1) { |
104 |
if (s == -1) { |
| 112 |
msgConfirm("Network error"); |
105 |
msgConfirm("Network error"); |
| 113 |
return NULL; |
106 |
return NULL; |
| 114 |
} |
107 |
} |
| 115 |
|
108 |
|
| 116 |
peer_in=gethostbyname(variable_get(VAR_HTTP_HOST)); |
109 |
peer_in=gethostbyname(variable_get(VAR_HTTP_HOST)); |
|
Lines 121-136
Link Here
|
| 121 |
|
114 |
|
| 122 |
rv=connect(s,(struct sockaddr *)&peer,sizeof(peer)); |
115 |
rv=connect(s,(struct sockaddr *)&peer,sizeof(peer)); |
| 123 |
if (rv == -1) { |
116 |
if (rv == -1) { |
| 124 |
msgConfirm("Couldn't connect to proxy %s:%s", |
117 |
msgConfirm("Couldn't connect to proxy %s:%s", |
| 125 |
variable_get(VAR_HTTP_HOST),variable_get(VAR_FTP_PORT)); |
118 |
variable_get(VAR_HTTP_HOST),variable_get(VAR_FTP_PORT)); |
| 126 |
return NULL; |
119 |
return NULL; |
| 127 |
} |
120 |
} |
| 128 |
|
121 |
|
| 129 |
sprintf(req,"GET ftp://%s:%s%s%s/%s%s HTTP/1.0\r\n\r\n", |
122 |
sprintf(req,"GET %s/%s/%s%s HTTP/1.0\r\n\r\n", |
| 130 |
variable_get(VAR_FTP_HOST), variable_get(VAR_FTP_PORT), |
123 |
variable_get(VAR_FTP_PATH), variable_get(VAR_RELNAME), |
| 131 |
"/pub/FreeBSD/", variable_get(VAR_RELNAME), |
124 |
file, variable_get(VAR_HTTP_FTP_MODE)); |
| 132 |
file,variable_get(VAR_HTTP_FTP_MODE)); |
125 |
|
| 133 |
msgDebug("sending http request: %s",req); |
126 |
if (isDebug()) { |
|
|
127 |
msgDebug("sending http request: %s",req); |
| 128 |
} |
| 134 |
write(s,req,strlen(req)); |
129 |
write(s,req,strlen(req)); |
| 135 |
|
130 |
|
| 136 |
/* |
131 |
/* |
|
Lines 142-182
Link Here
|
| 142 |
el=FALSE; |
137 |
el=FALSE; |
| 143 |
rv=read(s,cp,1); |
138 |
rv=read(s,cp,1); |
| 144 |
while (rv>0) { |
139 |
while (rv>0) { |
| 145 |
if ((*cp == '\012') && el) { |
140 |
if ((*cp == '\012') && el) { |
| 146 |
/* reached end of a header line */ |
141 |
/* reached end of a header line */ |
| 147 |
if (!strncmp(buf,"HTTP",4)) { |
142 |
if (!strncmp(buf,"HTTP",4)) { |
| 148 |
rv=strtol((char *)(buf+9),0,0); |
143 |
rv=strtol((char *)(buf+9),0,0); |
| 149 |
*(cp-1)='\0'; /* chop the CRLF off */ |
144 |
*(cp-1)='\0'; /* chop the CRLF off */ |
| 150 |
if (rv >= 500) { |
145 |
if (probe && (rv != 200)) { |
| 151 |
msgConfirm("Server error %s, you could try an other server",buf); |
146 |
return NULL; |
| 152 |
return NULL; |
147 |
} else if (rv >= 500) { |
| 153 |
} else if (rv == 404) { |
148 |
msgConfirm("Server error %s when sending %s, you could try an other server",buf, req); |
| 154 |
msgConfirm("%s was not found, maybe directory or release-version are wrong?",req); |
149 |
return NULL; |
| 155 |
return NULL; |
150 |
} else if (rv == 404) { |
| 156 |
} else if (rv >= 400) { |
151 |
msgConfirm("%s was not found, maybe directory or release-version are wrong?",req); |
| 157 |
msgConfirm("Client error %s, you could try an other server",buf); |
152 |
return NULL; |
| 158 |
return NULL; |
153 |
} else if (rv >= 400) { |
| 159 |
} else if (rv >= 300) { |
154 |
msgConfirm("Client error %s, you could try an other server",buf); |
| 160 |
msgConfirm("Error %s,",buf); |
155 |
return NULL; |
| 161 |
return NULL; |
156 |
} else if (rv >= 300) { |
| 162 |
} else if (rv != 200) { |
157 |
msgConfirm("Error %s,",buf); |
| 163 |
msgConfirm("Error %s when trying to fetch %s",buf,req); |
158 |
return NULL; |
| 164 |
return NULL; |
159 |
} else if (rv != 200) { |
| 165 |
} |
160 |
msgConfirm("Error %s when sending %s, you could try an other server",buf, req); |
| 166 |
} |
161 |
return NULL; |
| 167 |
/* ignore other headers */ |
162 |
} |
| 168 |
/* check for "\015\012" at beginning of line, i.e. end of headers */ |
163 |
} |
| 169 |
if ((cp-buf) == 1) |
164 |
/* ignore other headers */ |
| 170 |
break; |
165 |
/* check for "\015\012" at beginning of line, i.e. end of headers */ |
| 171 |
cp=buf; |
166 |
if ((cp-buf) == 1) |
| 172 |
rv=read(s,cp,1); |
167 |
break; |
| 173 |
} else { |
168 |
cp=buf; |
| 174 |
el=FALSE; |
169 |
rv=read(s,cp,1); |
| 175 |
if (*cp == '\015') |
170 |
} else { |
| 176 |
el=TRUE; |
171 |
el=FALSE; |
| 177 |
cp++; |
172 |
if (*cp == '\015') |
| 178 |
rv=read(s,cp,1); |
173 |
el=TRUE; |
| 179 |
} |
174 |
cp++; |
|
|
175 |
rv=read(s,cp,1); |
| 176 |
} |
| 180 |
} |
177 |
} |
| 181 |
fp=fdopen(s,"r"); |
178 |
fp=fdopen(s,"r"); |
| 182 |
return fp; |
179 |
return fp; |