]>
Commit | Line | Data |
---|---|---|
be4401bf AL |
1 | // -*- mode: cpp; mode: fold -*- |
2 | // Description /*{{{*/ | |
2cbcabd8 | 3 | // $Id: http.cc,v 1.59 2004/05/08 19:42:35 mdz Exp $ |
be4401bf AL |
4 | /* ###################################################################### |
5 | ||
1e3f4083 | 6 | HTTP Acquire Method - This is the HTTP acquire method for APT. |
be4401bf AL |
7 | |
8 | It uses HTTP/1.1 and many of the fancy options there-in, such as | |
e836f356 AL |
9 | pipelining, range, if-range and so on. |
10 | ||
11 | It is based on a doubly buffered select loop. A groupe of requests are | |
be4401bf AL |
12 | fed into a single output buffer that is constantly fed out the |
13 | socket. This provides ideal pipelining as in many cases all of the | |
14 | requests will fit into a single packet. The input socket is buffered | |
e836f356 | 15 | the same way and fed into the fd for the file (may be a pipe in future). |
be4401bf AL |
16 | |
17 | This double buffering provides fairly substantial transfer rates, | |
18 | compared to wget the http method is about 4% faster. Most importantly, | |
19 | when HTTP is compared with FTP as a protocol the speed difference is | |
20 | huge. In tests over the internet from two sites to llug (via ATM) this | |
21 | program got 230k/s sustained http transfer rates. FTP on the other | |
22 | hand topped out at 170k/s. That combined with the time to setup the | |
23 | FTP connection makes HTTP a vastly superior protocol. | |
24 | ||
25 | ##################################################################### */ | |
26 | /*}}}*/ | |
27 | // Include Files /*{{{*/ | |
ea542140 DK |
28 | #include <config.h> |
29 | ||
be4401bf AL |
30 | #include <apt-pkg/fileutl.h> |
31 | #include <apt-pkg/acquire-method.h> | |
472ff00e | 32 | #include <apt-pkg/configuration.h> |
be4401bf | 33 | #include <apt-pkg/error.h> |
63b1700f | 34 | #include <apt-pkg/hashes.h> |
592b7800 | 35 | #include <apt-pkg/netrc.h> |
453b82a3 | 36 | #include <apt-pkg/strutl.h> |
be4401bf | 37 | |
453b82a3 DK |
38 | #include <stddef.h> |
39 | #include <stdlib.h> | |
40 | #include <sys/select.h> | |
41 | #include <cstring> | |
be4401bf AL |
42 | #include <sys/stat.h> |
43 | #include <sys/time.h> | |
be4401bf AL |
44 | #include <unistd.h> |
45 | #include <stdio.h> | |
65a1e968 | 46 | #include <errno.h> |
42195eb2 | 47 | #include <iostream> |
be4401bf | 48 | |
59b46c41 | 49 | #include "config.h" |
0837bd25 | 50 | #include "connect.h" |
be4401bf | 51 | #include "http.h" |
ea542140 DK |
52 | |
53 | #include <apti18n.h> | |
be4401bf | 54 | /*}}}*/ |
42195eb2 | 55 | using namespace std; |
be4401bf | 56 | |
650faab0 DK |
57 | unsigned long long CircleBuf::BwReadLimit=0; |
58 | unsigned long long CircleBuf::BwTickReadData=0; | |
7c6e2dc7 MV |
59 | struct timeval CircleBuf::BwReadTick={0,0}; |
60 | const unsigned int CircleBuf::BW_HZ=10; | |
d3e8fbb3 | 61 | |
be4401bf AL |
62 | // CircleBuf::CircleBuf - Circular input buffer /*{{{*/ |
63 | // --------------------------------------------------------------------- | |
64 | /* */ | |
650faab0 | 65 | CircleBuf::CircleBuf(unsigned long long Size) : Size(Size), Hash(0) |
be4401bf AL |
66 | { |
67 | Buf = new unsigned char[Size]; | |
68 | Reset(); | |
7c6e2dc7 MV |
69 | |
70 | CircleBuf::BwReadLimit = _config->FindI("Acquire::http::Dl-Limit",0)*1024; | |
be4401bf AL |
71 | } |
72 | /*}}}*/ | |
73 | // CircleBuf::Reset - Reset to the default state /*{{{*/ | |
74 | // --------------------------------------------------------------------- | |
75 | /* */ | |
76 | void CircleBuf::Reset() | |
77 | { | |
78 | InP = 0; | |
79 | OutP = 0; | |
80 | StrPos = 0; | |
650faab0 | 81 | MaxGet = (unsigned long long)-1; |
be4401bf | 82 | OutQueue = string(); |
63b1700f | 83 | if (Hash != 0) |
be4401bf | 84 | { |
63b1700f AL |
85 | delete Hash; |
86 | Hash = new Hashes; | |
d3e8fbb3 DK |
87 | } |
88 | } | |
be4401bf AL |
89 | /*}}}*/ |
90 | // CircleBuf::Read - Read from a FD into the circular buffer /*{{{*/ | |
91 | // --------------------------------------------------------------------- | |
92 | /* This fills up the buffer with as much data as is in the FD, assuming it | |
93 | is non-blocking.. */ | |
94 | bool CircleBuf::Read(int Fd) | |
95 | { | |
96 | while (1) | |
97 | { | |
98 | // Woops, buffer is full | |
99 | if (InP - OutP == Size) | |
100 | return true; | |
7c6e2dc7 MV |
101 | |
102 | // what's left to read in this tick | |
9ce3cfc9 | 103 | unsigned long long const BwReadMax = CircleBuf::BwReadLimit/BW_HZ; |
7c6e2dc7 MV |
104 | |
105 | if(CircleBuf::BwReadLimit) { | |
106 | struct timeval now; | |
107 | gettimeofday(&now,0); | |
108 | ||
650faab0 | 109 | unsigned long long d = (now.tv_sec-CircleBuf::BwReadTick.tv_sec)*1000000 + |
7c6e2dc7 MV |
110 | now.tv_usec-CircleBuf::BwReadTick.tv_usec; |
111 | if(d > 1000000/BW_HZ) { | |
112 | CircleBuf::BwReadTick = now; | |
113 | CircleBuf::BwTickReadData = 0; | |
114 | } | |
115 | ||
116 | if(CircleBuf::BwTickReadData >= BwReadMax) { | |
117 | usleep(1000000/BW_HZ); | |
118 | return true; | |
119 | } | |
120 | } | |
121 | ||
be4401bf | 122 | // Write the buffer segment |
650faab0 | 123 | ssize_t Res; |
7c6e2dc7 MV |
124 | if(CircleBuf::BwReadLimit) { |
125 | Res = read(Fd,Buf + (InP%Size), | |
126 | BwReadMax > LeftRead() ? LeftRead() : BwReadMax); | |
127 | } else | |
128 | Res = read(Fd,Buf + (InP%Size),LeftRead()); | |
be4401bf | 129 | |
7c6e2dc7 MV |
130 | if(Res > 0 && BwReadLimit > 0) |
131 | CircleBuf::BwTickReadData += Res; | |
132 | ||
be4401bf AL |
133 | if (Res == 0) |
134 | return false; | |
135 | if (Res < 0) | |
136 | { | |
137 | if (errno == EAGAIN) | |
138 | return true; | |
139 | return false; | |
140 | } | |
141 | ||
142 | if (InP == 0) | |
143 | gettimeofday(&Start,0); | |
144 | InP += Res; | |
145 | } | |
146 | } | |
147 | /*}}}*/ | |
148 | // CircleBuf::Read - Put the string into the buffer /*{{{*/ | |
149 | // --------------------------------------------------------------------- | |
150 | /* This will hold the string in and fill the buffer with it as it empties */ | |
151 | bool CircleBuf::Read(string Data) | |
152 | { | |
153 | OutQueue += Data; | |
154 | FillOut(); | |
155 | return true; | |
156 | } | |
157 | /*}}}*/ | |
158 | // CircleBuf::FillOut - Fill the buffer from the output queue /*{{{*/ | |
159 | // --------------------------------------------------------------------- | |
160 | /* */ | |
161 | void CircleBuf::FillOut() | |
162 | { | |
163 | if (OutQueue.empty() == true) | |
164 | return; | |
165 | while (1) | |
166 | { | |
167 | // Woops, buffer is full | |
168 | if (InP - OutP == Size) | |
169 | return; | |
170 | ||
171 | // Write the buffer segment | |
650faab0 | 172 | unsigned long long Sz = LeftRead(); |
be4401bf AL |
173 | if (OutQueue.length() - StrPos < Sz) |
174 | Sz = OutQueue.length() - StrPos; | |
42195eb2 | 175 | memcpy(Buf + (InP%Size),OutQueue.c_str() + StrPos,Sz); |
be4401bf AL |
176 | |
177 | // Advance | |
178 | StrPos += Sz; | |
179 | InP += Sz; | |
180 | if (OutQueue.length() == StrPos) | |
181 | { | |
182 | StrPos = 0; | |
183 | OutQueue = ""; | |
184 | return; | |
185 | } | |
186 | } | |
187 | } | |
188 | /*}}}*/ | |
189 | // CircleBuf::Write - Write from the buffer into a FD /*{{{*/ | |
190 | // --------------------------------------------------------------------- | |
191 | /* This empties the buffer into the FD. */ | |
192 | bool CircleBuf::Write(int Fd) | |
193 | { | |
194 | while (1) | |
195 | { | |
196 | FillOut(); | |
197 | ||
198 | // Woops, buffer is empty | |
199 | if (OutP == InP) | |
200 | return true; | |
201 | ||
202 | if (OutP == MaxGet) | |
203 | return true; | |
204 | ||
205 | // Write the buffer segment | |
650faab0 | 206 | ssize_t Res; |
be4401bf AL |
207 | Res = write(Fd,Buf + (OutP%Size),LeftWrite()); |
208 | ||
209 | if (Res == 0) | |
210 | return false; | |
211 | if (Res < 0) | |
212 | { | |
213 | if (errno == EAGAIN) | |
214 | return true; | |
215 | ||
216 | return false; | |
217 | } | |
218 | ||
63b1700f AL |
219 | if (Hash != 0) |
220 | Hash->Add(Buf + (OutP%Size),Res); | |
be4401bf AL |
221 | |
222 | OutP += Res; | |
223 | } | |
224 | } | |
225 | /*}}}*/ | |
226 | // CircleBuf::WriteTillEl - Write from the buffer to a string /*{{{*/ | |
227 | // --------------------------------------------------------------------- | |
228 | /* This copies till the first empty line */ | |
229 | bool CircleBuf::WriteTillEl(string &Data,bool Single) | |
230 | { | |
231 | // We cheat and assume it is unneeded to have more than one buffer load | |
650faab0 | 232 | for (unsigned long long I = OutP; I < InP; I++) |
be4401bf AL |
233 | { |
234 | if (Buf[I%Size] != '\n') | |
235 | continue; | |
2cbcabd8 | 236 | ++I; |
be4401bf AL |
237 | |
238 | if (Single == false) | |
239 | { | |
2cbcabd8 AL |
240 | if (I < InP && Buf[I%Size] == '\r') |
241 | ++I; | |
927c393f MV |
242 | if (I >= InP || Buf[I%Size] != '\n') |
243 | continue; | |
244 | ++I; | |
be4401bf AL |
245 | } |
246 | ||
be4401bf AL |
247 | Data = ""; |
248 | while (OutP < I) | |
249 | { | |
650faab0 | 250 | unsigned long long Sz = LeftWrite(); |
be4401bf AL |
251 | if (Sz == 0) |
252 | return false; | |
927c393f | 253 | if (I - OutP < Sz) |
be4401bf AL |
254 | Sz = I - OutP; |
255 | Data += string((char *)(Buf + (OutP%Size)),Sz); | |
256 | OutP += Sz; | |
257 | } | |
258 | return true; | |
259 | } | |
260 | return false; | |
261 | } | |
262 | /*}}}*/ | |
263 | // CircleBuf::Stats - Print out stats information /*{{{*/ | |
264 | // --------------------------------------------------------------------- | |
265 | /* */ | |
266 | void CircleBuf::Stats() | |
267 | { | |
268 | if (InP == 0) | |
269 | return; | |
270 | ||
271 | struct timeval Stop; | |
272 | gettimeofday(&Stop,0); | |
273 | /* float Diff = Stop.tv_sec - Start.tv_sec + | |
274 | (float)(Stop.tv_usec - Start.tv_usec)/1000000; | |
275 | clog << "Got " << InP << " in " << Diff << " at " << InP/Diff << endl;*/ | |
276 | } | |
277 | /*}}}*/ | |
472ff00e DK |
278 | CircleBuf::~CircleBuf() |
279 | { | |
280 | delete [] Buf; | |
281 | delete Hash; | |
282 | } | |
be4401bf | 283 | |
7330f4df DK |
284 | // HttpServerState::HttpServerState - Constructor /*{{{*/ |
285 | HttpServerState::HttpServerState(URI Srv,HttpMethod *Owner) : ServerState(Srv, Owner), In(64*1024), Out(4*1024) | |
be4401bf | 286 | { |
7330f4df | 287 | TimeOut = _config->FindI("Acquire::http::Timeout",TimeOut); |
be4401bf AL |
288 | Reset(); |
289 | } | |
290 | /*}}}*/ | |
7330f4df | 291 | // HttpServerState::Open - Open a connection to the server /*{{{*/ |
be4401bf AL |
292 | // --------------------------------------------------------------------- |
293 | /* This opens a connection to the server. */ | |
7330f4df | 294 | bool HttpServerState::Open() |
be4401bf | 295 | { |
92e889c8 AL |
296 | // Use the already open connection if possible. |
297 | if (ServerFd != -1) | |
298 | return true; | |
299 | ||
be4401bf | 300 | Close(); |
492f957a AL |
301 | In.Reset(); |
302 | Out.Reset(); | |
e836f356 AL |
303 | Persistent = true; |
304 | ||
492f957a | 305 | // Determine the proxy setting |
788a8f42 EL |
306 | string SpecificProxy = _config->Find("Acquire::http::Proxy::" + ServerName.Host); |
307 | if (!SpecificProxy.empty()) | |
492f957a | 308 | { |
788a8f42 EL |
309 | if (SpecificProxy == "DIRECT") |
310 | Proxy = ""; | |
311 | else | |
312 | Proxy = SpecificProxy; | |
352c2768 | 313 | } |
492f957a | 314 | else |
788a8f42 EL |
315 | { |
316 | string DefProxy = _config->Find("Acquire::http::Proxy"); | |
317 | if (!DefProxy.empty()) | |
318 | { | |
319 | Proxy = DefProxy; | |
320 | } | |
321 | else | |
322 | { | |
323 | char* result = getenv("http_proxy"); | |
324 | Proxy = result ? result : ""; | |
325 | } | |
326 | } | |
352c2768 | 327 | |
f8081133 | 328 | // Parse no_proxy, a , separated list of domains |
9e2a06ff AL |
329 | if (getenv("no_proxy") != 0) |
330 | { | |
f8081133 AL |
331 | if (CheckDomainList(ServerName.Host,getenv("no_proxy")) == true) |
332 | Proxy = ""; | |
333 | } | |
334 | ||
492f957a | 335 | // Determine what host and port to use based on the proxy settings |
934b6582 | 336 | int Port = 0; |
492f957a | 337 | string Host; |
dd1fd92b | 338 | if (Proxy.empty() == true || Proxy.Host.empty() == true) |
be4401bf | 339 | { |
92e889c8 AL |
340 | if (ServerName.Port != 0) |
341 | Port = ServerName.Port; | |
be4401bf AL |
342 | Host = ServerName.Host; |
343 | } | |
344 | else | |
345 | { | |
92e889c8 AL |
346 | if (Proxy.Port != 0) |
347 | Port = Proxy.Port; | |
be4401bf AL |
348 | Host = Proxy.Host; |
349 | } | |
350 | ||
0837bd25 | 351 | // Connect to the remote server |
9505213b | 352 | if (Connect(Host,Port,"http",80,ServerFd,TimeOut,Owner) == false) |
0837bd25 | 353 | return false; |
3000ccea | 354 | |
be4401bf AL |
355 | return true; |
356 | } | |
357 | /*}}}*/ | |
7330f4df | 358 | // HttpServerState::Close - Close a connection to the server /*{{{*/ |
be4401bf AL |
359 | // --------------------------------------------------------------------- |
360 | /* */ | |
7330f4df | 361 | bool HttpServerState::Close() |
be4401bf AL |
362 | { |
363 | close(ServerFd); | |
364 | ServerFd = -1; | |
be4401bf AL |
365 | return true; |
366 | } | |
367 | /*}}}*/ | |
7330f4df DK |
368 | // HttpServerState::RunData - Transfer the data from the socket /*{{{*/ |
369 | bool HttpServerState::RunData(FileFd * const File) | |
be4401bf AL |
370 | { |
371 | State = Data; | |
372 | ||
373 | // Chunked transfer encoding is fun.. | |
374 | if (Encoding == Chunked) | |
375 | { | |
376 | while (1) | |
377 | { | |
378 | // Grab the block size | |
379 | bool Last = true; | |
380 | string Data; | |
381 | In.Limit(-1); | |
382 | do | |
383 | { | |
384 | if (In.WriteTillEl(Data,true) == true) | |
385 | break; | |
386 | } | |
7330f4df | 387 | while ((Last = Go(false, File)) == true); |
be4401bf AL |
388 | |
389 | if (Last == false) | |
390 | return false; | |
391 | ||
392 | // See if we are done | |
650faab0 | 393 | unsigned long long Len = strtoull(Data.c_str(),0,16); |
be4401bf AL |
394 | if (Len == 0) |
395 | { | |
396 | In.Limit(-1); | |
397 | ||
398 | // We have to remove the entity trailer | |
399 | Last = true; | |
400 | do | |
401 | { | |
402 | if (In.WriteTillEl(Data,true) == true && Data.length() <= 2) | |
403 | break; | |
404 | } | |
7330f4df | 405 | while ((Last = Go(false, File)) == true); |
be4401bf AL |
406 | if (Last == false) |
407 | return false; | |
e1b96638 | 408 | return !_error->PendingError(); |
be4401bf AL |
409 | } |
410 | ||
411 | // Transfer the block | |
412 | In.Limit(Len); | |
7330f4df | 413 | while (Go(true, File) == true) |
be4401bf AL |
414 | if (In.IsLimit() == true) |
415 | break; | |
416 | ||
417 | // Error | |
418 | if (In.IsLimit() == false) | |
419 | return false; | |
420 | ||
421 | // The server sends an extra new line before the next block specifier.. | |
422 | In.Limit(-1); | |
423 | Last = true; | |
424 | do | |
425 | { | |
426 | if (In.WriteTillEl(Data,true) == true) | |
427 | break; | |
428 | } | |
7330f4df | 429 | while ((Last = Go(false, File)) == true); |
be4401bf AL |
430 | if (Last == false) |
431 | return false; | |
92e889c8 | 432 | } |
be4401bf AL |
433 | } |
434 | else | |
435 | { | |
436 | /* Closes encoding is used when the server did not specify a size, the | |
437 | loss of the connection means we are done */ | |
438 | if (Encoding == Closes) | |
439 | In.Limit(-1); | |
440 | else | |
441 | In.Limit(Size - StartPos); | |
442 | ||
443 | // Just transfer the whole block. | |
444 | do | |
445 | { | |
446 | if (In.IsLimit() == false) | |
447 | continue; | |
448 | ||
449 | In.Limit(-1); | |
e1b96638 | 450 | return !_error->PendingError(); |
be4401bf | 451 | } |
7330f4df | 452 | while (Go(true, File) == true); |
be4401bf AL |
453 | } |
454 | ||
7330f4df | 455 | return Owner->Flush() && !_error->PendingError(); |
be4401bf AL |
456 | } |
457 | /*}}}*/ | |
7330f4df | 458 | bool HttpServerState::ReadHeaderLines(std::string &Data) /*{{{*/ |
be4401bf | 459 | { |
7330f4df DK |
460 | return In.WriteTillEl(Data); |
461 | } | |
462 | /*}}}*/ | |
463 | bool HttpServerState::LoadNextResponse(bool const ToFile, FileFd * const File)/*{{{*/ | |
464 | { | |
465 | return Go(ToFile, File); | |
466 | } | |
467 | /*}}}*/ | |
468 | bool HttpServerState::WriteResponse(const std::string &Data) /*{{{*/ | |
469 | { | |
470 | return Out.Read(Data); | |
471 | } | |
472 | /*}}}*/ | |
a02db58f | 473 | APT_PURE bool HttpServerState::IsOpen() /*{{{*/ |
7330f4df DK |
474 | { |
475 | return (ServerFd != -1); | |
476 | } | |
477 | /*}}}*/ | |
478 | bool HttpServerState::InitHashes(FileFd &File) /*{{{*/ | |
479 | { | |
480 | delete In.Hash; | |
481 | In.Hash = new Hashes; | |
30456e14 | 482 | |
7330f4df | 483 | // Set the expected size and read file for the hashes |
3de8f956 DK |
484 | File.Truncate(StartPos); |
485 | return In.Hash->AddFD(File, StartPos); | |
7330f4df DK |
486 | } |
487 | /*}}}*/ | |
a02db58f | 488 | APT_PURE Hashes * HttpServerState::GetHashes() /*{{{*/ |
7330f4df DK |
489 | { |
490 | return In.Hash; | |
491 | } | |
492 | /*}}}*/ | |
493 | // HttpServerState::Die - The server has closed the connection. /*{{{*/ | |
494 | bool HttpServerState::Die(FileFd &File) | |
495 | { | |
496 | unsigned int LErrno = errno; | |
be4401bf | 497 | |
7330f4df DK |
498 | // Dump the buffer to the file |
499 | if (State == ServerState::Data) | |
be4401bf | 500 | { |
7330f4df DK |
501 | // on GNU/kFreeBSD, apt dies on /dev/null because non-blocking |
502 | // can't be set | |
503 | if (File.Name() != "/dev/null") | |
504 | SetNonBlock(File.Fd(),false); | |
505 | while (In.WriteSpace() == true) | |
be4401bf | 506 | { |
7330f4df DK |
507 | if (In.Write(File.Fd()) == false) |
508 | return _error->Errno("write",_("Error writing to the file")); | |
e836f356 | 509 | |
7330f4df DK |
510 | // Done |
511 | if (In.IsLimit() == true) | |
512 | return true; | |
e836f356 | 513 | } |
7330f4df | 514 | } |
b2e465d6 | 515 | |
7330f4df DK |
516 | // See if this is because the server finished the data stream |
517 | if (In.IsLimit() == false && State != HttpServerState::Header && | |
518 | Encoding != HttpServerState::Closes) | |
be4401bf | 519 | { |
7330f4df DK |
520 | Close(); |
521 | if (LErrno == 0) | |
522 | return _error->Error(_("Error reading from server. Remote end closed connection")); | |
523 | errno = LErrno; | |
524 | return _error->Errno("read",_("Error reading from server")); | |
be4401bf | 525 | } |
7330f4df | 526 | else |
92e889c8 | 527 | { |
7330f4df DK |
528 | In.Limit(-1); |
529 | ||
530 | // Nothing left in the buffer | |
531 | if (In.WriteSpace() == false) | |
532 | return false; | |
533 | ||
534 | // We may have got multiple responses back in one packet.. | |
535 | Close(); | |
92e889c8 AL |
536 | return true; |
537 | } | |
331e8396 | 538 | |
7330f4df DK |
539 | return false; |
540 | } | |
541 | /*}}}*/ | |
542 | // HttpServerState::Flush - Dump the buffer into the file /*{{{*/ | |
543 | // --------------------------------------------------------------------- | |
544 | /* This takes the current input buffer from the Server FD and writes it | |
545 | into the file */ | |
546 | bool HttpServerState::Flush(FileFd * const File) | |
547 | { | |
548 | if (File != NULL) | |
549 | { | |
550 | // on GNU/kFreeBSD, apt dies on /dev/null because non-blocking | |
551 | // can't be set | |
552 | if (File->Name() != "/dev/null") | |
553 | SetNonBlock(File->Fd(),false); | |
554 | if (In.WriteSpace() == false) | |
555 | return true; | |
556 | ||
557 | while (In.WriteSpace() == true) | |
331e8396 | 558 | { |
7330f4df DK |
559 | if (In.Write(File->Fd()) == false) |
560 | return _error->Errno("write",_("Error writing to file")); | |
561 | if (In.IsLimit() == true) | |
562 | return true; | |
331e8396 | 563 | } |
7330f4df DK |
564 | |
565 | if (In.IsLimit() == true || Encoding == ServerState::Closes) | |
566 | return true; | |
be4401bf | 567 | } |
7330f4df DK |
568 | return false; |
569 | } | |
570 | /*}}}*/ | |
571 | // HttpServerState::Go - Run a single loop /*{{{*/ | |
572 | // --------------------------------------------------------------------- | |
573 | /* This runs the select loop over the server FDs, Output file FDs and | |
574 | stdin. */ | |
575 | bool HttpServerState::Go(bool ToFile, FileFd * const File) | |
576 | { | |
577 | // Server has closed the connection | |
578 | if (ServerFd == -1 && (In.WriteSpace() == false || | |
579 | ToFile == false)) | |
580 | return false; | |
581 | ||
582 | fd_set rfds,wfds; | |
583 | FD_ZERO(&rfds); | |
584 | FD_ZERO(&wfds); | |
585 | ||
586 | /* Add the server. We only send more requests if the connection will | |
587 | be persisting */ | |
588 | if (Out.WriteSpace() == true && ServerFd != -1 | |
589 | && Persistent == true) | |
590 | FD_SET(ServerFd,&wfds); | |
591 | if (In.ReadSpace() == true && ServerFd != -1) | |
592 | FD_SET(ServerFd,&rfds); | |
be4401bf | 593 | |
7330f4df DK |
594 | // Add the file |
595 | int FileFD = -1; | |
596 | if (File != NULL) | |
597 | FileFD = File->Fd(); | |
598 | ||
599 | if (In.WriteSpace() == true && ToFile == true && FileFD != -1) | |
600 | FD_SET(FileFD,&wfds); | |
601 | ||
602 | // Add stdin | |
603 | if (_config->FindB("Acquire::http::DependOnSTDIN", true) == true) | |
604 | FD_SET(STDIN_FILENO,&rfds); | |
605 | ||
606 | // Figure out the max fd | |
607 | int MaxFd = FileFD; | |
608 | if (MaxFd < ServerFd) | |
609 | MaxFd = ServerFd; | |
610 | ||
611 | // Select | |
612 | struct timeval tv; | |
613 | tv.tv_sec = TimeOut; | |
614 | tv.tv_usec = 0; | |
615 | int Res = 0; | |
616 | if ((Res = select(MaxFd+1,&rfds,&wfds,0,&tv)) < 0) | |
be4401bf | 617 | { |
7330f4df DK |
618 | if (errno == EINTR) |
619 | return true; | |
620 | return _error->Errno("select",_("Select failed")); | |
be4401bf | 621 | } |
7330f4df DK |
622 | |
623 | if (Res == 0) | |
e836f356 | 624 | { |
7330f4df DK |
625 | _error->Error(_("Connection timed out")); |
626 | return Die(*File); | |
e836f356 AL |
627 | } |
628 | ||
7330f4df DK |
629 | // Handle server IO |
630 | if (ServerFd != -1 && FD_ISSET(ServerFd,&rfds)) | |
be4401bf | 631 | { |
7330f4df DK |
632 | errno = 0; |
633 | if (In.Read(ServerFd) == false) | |
634 | return Die(*File); | |
635 | } | |
636 | ||
637 | if (ServerFd != -1 && FD_ISSET(ServerFd,&wfds)) | |
638 | { | |
639 | errno = 0; | |
640 | if (Out.Write(ServerFd) == false) | |
641 | return Die(*File); | |
be4401bf AL |
642 | } |
643 | ||
7330f4df DK |
644 | // Send data to the file |
645 | if (FileFD != -1 && FD_ISSET(FileFD,&wfds)) | |
15d7e515 | 646 | { |
7330f4df DK |
647 | if (In.Write(FileFD) == false) |
648 | return _error->Errno("write",_("Error writing to output file")); | |
15d7e515 MV |
649 | } |
650 | ||
7330f4df DK |
651 | // Handle commands from APT |
652 | if (FD_ISSET(STDIN_FILENO,&rfds)) | |
653 | { | |
654 | if (Owner->Run(true) != -1) | |
655 | exit(100); | |
656 | } | |
657 | ||
be4401bf AL |
658 | return true; |
659 | } | |
660 | /*}}}*/ | |
661 | ||
662 | // HttpMethod::SendReq - Send the HTTP request /*{{{*/ | |
663 | // --------------------------------------------------------------------- | |
664 | /* This places the http request in the outbound buffer */ | |
7330f4df | 665 | void HttpMethod::SendReq(FetchItem *Itm) |
be4401bf AL |
666 | { |
667 | URI Uri = Itm->Uri; | |
c1a22377 | 668 | |
be4401bf | 669 | // The HTTP server expects a hostname with a trailing :port |
c1a22377 | 670 | char Buf[1000]; |
5b63d2a9 MV |
671 | string ProperHost; |
672 | ||
673 | if (Uri.Host.find(':') != string::npos) | |
674 | ProperHost = '[' + Uri.Host + ']'; | |
675 | else | |
676 | ProperHost = Uri.Host; | |
be4401bf AL |
677 | if (Uri.Port != 0) |
678 | { | |
679 | sprintf(Buf,":%u",Uri.Port); | |
680 | ProperHost += Buf; | |
681 | } | |
682 | ||
c1a22377 AL |
683 | // Just in case. |
684 | if (Itm->Uri.length() >= sizeof(Buf)) | |
685 | abort(); | |
f2380a78 DK |
686 | |
687 | /* RFC 2616 ยง5.1.2 requires absolute URIs for requests to proxies, | |
688 | but while its a must for all servers to accept absolute URIs, | |
689 | it is assumed clients will sent an absolute path for non-proxies */ | |
690 | std::string requesturi; | |
7330f4df | 691 | if (Server->Proxy.empty() == true || Server->Proxy.Host.empty()) |
f2380a78 DK |
692 | requesturi = Uri.Path; |
693 | else | |
694 | requesturi = Itm->Uri; | |
695 | ||
696 | // The "+" is encoded as a workaround for a amazon S3 bug | |
697 | // see LP bugs #1003633 and #1086997. | |
698 | requesturi = QuoteString(requesturi, "+~ "); | |
699 | ||
2b9c9b7f RG |
700 | /* Build the request. No keep-alive is included as it is the default |
701 | in 1.1, can cause problems with proxies, and we are an HTTP/1.1 | |
702 | client anyway. | |
703 | C.f. https://tools.ietf.org/wg/httpbis/trac/ticket/158 */ | |
2b9c9b7f | 704 | sprintf(Buf,"GET %s HTTP/1.1\r\nHost: %s\r\n", |
f2380a78 DK |
705 | requesturi.c_str(),ProperHost.c_str()); |
706 | ||
c9cd3b70 MV |
707 | // generate a cache control header (if needed) |
708 | if (_config->FindB("Acquire::http::No-Cache",false) == true) | |
709 | { | |
710 | strcat(Buf,"Cache-Control: no-cache\r\nPragma: no-cache\r\n"); | |
711 | } | |
712 | else | |
713 | { | |
714 | if (Itm->IndexFile == true) | |
c1a22377 | 715 | { |
c9cd3b70 MV |
716 | sprintf(Buf+strlen(Buf),"Cache-Control: max-age=%u\r\n", |
717 | _config->FindI("Acquire::http::Max-Age",0)); | |
718 | } | |
719 | else | |
720 | { | |
721 | if (_config->FindB("Acquire::http::No-Store",false) == true) | |
722 | strcat(Buf,"Cache-Control: no-store\r\n"); | |
c1a22377 AL |
723 | } |
724 | } | |
106e6740 | 725 | |
6f4501f9 | 726 | // If we ask for uncompressed files servers might respond with content- |
1e3f4083 | 727 | // negotiation which lets us end up with compressed files we do not support, |
6f4501f9 DK |
728 | // see 657029, 657560 and co, so if we have no extension on the request |
729 | // ask for text only. As a sidenote: If there is nothing to negotate servers | |
730 | // seem to be nice and ignore it. | |
731 | if (_config->FindB("Acquire::http::SendAccept", true) == true) | |
732 | { | |
733 | size_t const filepos = Itm->Uri.find_last_of('/'); | |
734 | string const file = Itm->Uri.substr(filepos + 1); | |
735 | if (flExtension(file) == file) | |
736 | strcat(Buf,"Accept: text/*\r\n"); | |
737 | } | |
738 | ||
be4401bf | 739 | string Req = Buf; |
492f957a | 740 | |
be4401bf AL |
741 | // Check for a partial file |
742 | struct stat SBuf; | |
743 | if (stat(Itm->DestFile.c_str(),&SBuf) >= 0 && SBuf.st_size > 0) | |
744 | { | |
745 | // In this case we send an if-range query with a range header | |
78c72d0c | 746 | sprintf(Buf,"Range: bytes=%lli-\r\nIf-Range: %s\r\n",(long long)SBuf.st_size, |
be4401bf AL |
747 | TimeRFC1123(SBuf.st_mtime).c_str()); |
748 | Req += Buf; | |
749 | } | |
750 | else | |
751 | { | |
752 | if (Itm->LastModified != 0) | |
753 | { | |
754 | sprintf(Buf,"If-Modified-Since: %s\r\n",TimeRFC1123(Itm->LastModified).c_str()); | |
755 | Req += Buf; | |
756 | } | |
757 | } | |
758 | ||
7330f4df | 759 | if (Server->Proxy.User.empty() == false || Server->Proxy.Password.empty() == false) |
8d64c395 | 760 | Req += string("Proxy-Authorization: Basic ") + |
7330f4df | 761 | Base64Encode(Server->Proxy.User + ":" + Server->Proxy.Password) + "\r\n"; |
be4401bf | 762 | |
1de1f703 | 763 | maybe_add_auth (Uri, _config->FindFile("Dir::Etc::netrc")); |
b2e465d6 | 764 | if (Uri.User.empty() == false || Uri.Password.empty() == false) |
592b7800 | 765 | { |
b2e465d6 AL |
766 | Req += string("Authorization: Basic ") + |
767 | Base64Encode(Uri.User + ":" + Uri.Password) + "\r\n"; | |
592b7800 | 768 | } |
9f542bae | 769 | Req += "User-Agent: " + _config->Find("Acquire::http::User-Agent", |
335e2c82 | 770 | "Debian APT-HTTP/1.3 (" PACKAGE_VERSION ")") + "\r\n\r\n"; |
c98b1307 AL |
771 | |
772 | if (Debug == true) | |
773 | cerr << Req << endl; | |
c1a22377 | 774 | |
7330f4df | 775 | Server->WriteResponse(Req); |
be4401bf AL |
776 | } |
777 | /*}}}*/ | |
85f72a56 AL |
778 | // HttpMethod::Configuration - Handle a configuration message /*{{{*/ |
779 | // --------------------------------------------------------------------- | |
780 | /* We stash the desired pipeline depth */ | |
781 | bool HttpMethod::Configuration(string Message) | |
782 | { | |
7330f4df | 783 | if (ServerMethod::Configuration(Message) == false) |
85f72a56 | 784 | return false; |
7330f4df | 785 | |
15d7e515 | 786 | AllowRedirect = _config->FindB("Acquire::http::AllowRedirect",true); |
30456e14 AL |
787 | PipelineDepth = _config->FindI("Acquire::http::Pipeline-Depth", |
788 | PipelineDepth); | |
c98b1307 | 789 | Debug = _config->FindB("Debug::Acquire::http",false); |
bd49b02c MV |
790 | |
791 | // Get the proxy to use | |
792 | AutoDetectProxy(); | |
793 | ||
85f72a56 AL |
794 | return true; |
795 | } | |
796 | /*}}}*/ | |
bd49b02c MV |
797 | // HttpMethod::AutoDetectProxy - auto detect proxy /*{{{*/ |
798 | // --------------------------------------------------------------------- | |
799 | /* */ | |
800 | bool HttpMethod::AutoDetectProxy() | |
801 | { | |
c08cf1dc MV |
802 | // option is "Acquire::http::Proxy-Auto-Detect" but we allow the old |
803 | // name without the dash ("-") | |
804 | AutoDetectProxyCmd = _config->Find("Acquire::http::Proxy-Auto-Detect", | |
805 | _config->Find("Acquire::http::ProxyAutoDetect")); | |
806 | ||
bd49b02c MV |
807 | if (AutoDetectProxyCmd.empty()) |
808 | return true; | |
809 | ||
810 | if (Debug) | |
811 | clog << "Using auto proxy detect command: " << AutoDetectProxyCmd << endl; | |
812 | ||
813 | int Pipes[2] = {-1,-1}; | |
814 | if (pipe(Pipes) != 0) | |
815 | return _error->Errno("pipe", "Failed to create Pipe"); | |
816 | ||
817 | pid_t Process = ExecFork(); | |
818 | if (Process == 0) | |
819 | { | |
08ded4d6 | 820 | close(Pipes[0]); |
bd49b02c MV |
821 | dup2(Pipes[1],STDOUT_FILENO); |
822 | SetCloseExec(STDOUT_FILENO,false); | |
08ded4d6 | 823 | |
bd49b02c MV |
824 | const char *Args[2]; |
825 | Args[0] = AutoDetectProxyCmd.c_str(); | |
826 | Args[1] = 0; | |
827 | execv(Args[0],(char **)Args); | |
828 | cerr << "Failed to exec method " << Args[0] << endl; | |
829 | _exit(100); | |
830 | } | |
831 | char buf[512]; | |
832 | int InFd = Pipes[0]; | |
08ded4d6 | 833 | close(Pipes[1]); |
c1042000 | 834 | int res = read(InFd, buf, sizeof(buf)-1); |
08ded4d6 MV |
835 | ExecWait(Process, "ProxyAutoDetect", true); |
836 | ||
837 | if (res < 0) | |
bd49b02c | 838 | return _error->Errno("read", "Failed to read"); |
08ded4d6 MV |
839 | if (res == 0) |
840 | return _error->Warning("ProxyAutoDetect returned no data"); | |
841 | ||
842 | // add trailing \0 | |
843 | buf[res] = 0; | |
844 | ||
bd49b02c MV |
845 | if (Debug) |
846 | clog << "auto detect command returned: '" << buf << "'" << endl; | |
847 | ||
848 | if (strstr(buf, "http://") == buf) | |
849 | _config->Set("Acquire::http::proxy", _strstrip(buf)); | |
850 | ||
851 | return true; | |
852 | } | |
853 | /*}}}*/ | |
7330f4df DK |
854 | ServerState * HttpMethod::CreateServerState(URI uri) /*{{{*/ |
855 | { | |
856 | return new HttpServerState(uri, this); | |
857 | } | |
858 | /*}}}*/ | |
fd46d305 DK |
859 | void HttpMethod::RotateDNS() /*{{{*/ |
860 | { | |
861 | ::RotateDNS(); | |
862 | } | |
863 | /*}}}*/ |