]> git.saurik.com Git - apt.git/blame - methods/server.cc
correct some style/performance/warnings from cppcheck
[apt.git] / methods / server.cc
CommitLineData
7330f4df
DK
1// -*- mode: cpp; mode: fold -*-
2// Description /*{{{*/
3/* ######################################################################
4
5 HTTP and HTTPS share a lot of common code and these classes are
6 exactly the dumping ground for this common code
7
8 ##################################################################### */
9 /*}}}*/
10// Include Files /*{{{*/
11#include <config.h>
12
13#include <apt-pkg/fileutl.h>
14#include <apt-pkg/acquire-method.h>
15#include <apt-pkg/configuration.h>
16#include <apt-pkg/error.h>
17#include <apt-pkg/hashes.h>
18#include <apt-pkg/netrc.h>
19
9ce3cfc9 20#include <fcntl.h>
7330f4df
DK
21#include <sys/stat.h>
22#include <sys/time.h>
7330f4df
DK
23#include <unistd.h>
24#include <signal.h>
25#include <stdio.h>
26#include <errno.h>
27#include <string.h>
28#include <climits>
29#include <iostream>
30#include <map>
31
32// Internet stuff
33#include <netdb.h>
34
35#include "config.h"
36#include "connect.h"
37#include "rfc2553emu.h"
38#include "http.h"
39
40#include <apti18n.h>
41 /*}}}*/
42using namespace std;
43
44string ServerMethod::FailFile;
45int ServerMethod::FailFd = -1;
46time_t ServerMethod::FailTime = 0;
47
48// ServerState::RunHeaders - Get the headers before the data /*{{{*/
49// ---------------------------------------------------------------------
50/* Returns 0 if things are OK, 1 if an IO error occurred and 2 if a header
51 parse error occurred */
52ServerState::RunHeadersResult ServerState::RunHeaders(FileFd * const File)
53{
54 State = Header;
55
56 Owner->Status(_("Waiting for headers"));
57
58 Major = 0;
59 Minor = 0;
60 Result = 0;
61 Size = 0;
62 StartPos = 0;
63 Encoding = Closes;
64 HaveContent = false;
65 time(&Date);
66
67 do
68 {
69 string Data;
70 if (ReadHeaderLines(Data) == false)
71 continue;
72
73 if (Owner->Debug == true)
74 clog << Data;
75
76 for (string::const_iterator I = Data.begin(); I < Data.end(); ++I)
77 {
78 string::const_iterator J = I;
79 for (; J != Data.end() && *J != '\n' && *J != '\r'; ++J);
80 if (HeaderLine(string(I,J)) == false)
81 return RUN_HEADERS_PARSE_ERROR;
82 I = J;
83 }
84
85 // 100 Continue is a Nop...
86 if (Result == 100)
87 continue;
88
89 // Tidy up the connection persistance state.
90 if (Encoding == Closes && HaveContent == true)
91 Persistent = false;
92
93 return RUN_HEADERS_OK;
94 }
95 while (LoadNextResponse(false, File) == true);
96
97 return RUN_HEADERS_IO_ERROR;
98}
99 /*}}}*/
100// ServerState::HeaderLine - Process a header line /*{{{*/
101// ---------------------------------------------------------------------
102/* */
103bool ServerState::HeaderLine(string Line)
104{
105 if (Line.empty() == true)
106 return true;
107
108 string::size_type Pos = Line.find(' ');
109 if (Pos == string::npos || Pos+1 > Line.length())
110 {
111 // Blah, some servers use "connection:closes", evil.
112 Pos = Line.find(':');
113 if (Pos == string::npos || Pos + 2 > Line.length())
114 return _error->Error(_("Bad header line"));
115 Pos++;
116 }
117
118 // Parse off any trailing spaces between the : and the next word.
119 string::size_type Pos2 = Pos;
120 while (Pos2 < Line.length() && isspace(Line[Pos2]) != 0)
121 Pos2++;
122
123 string Tag = string(Line,0,Pos);
124 string Val = string(Line,Pos2);
125
126 if (stringcasecmp(Tag.c_str(),Tag.c_str()+4,"HTTP") == 0)
127 {
128 // Evil servers return no version
129 if (Line[4] == '/')
130 {
131 int const elements = sscanf(Line.c_str(),"HTTP/%3u.%3u %3u%359[^\n]",&Major,&Minor,&Result,Code);
132 if (elements == 3)
133 {
134 Code[0] = '\0';
135 if (Owner->Debug == true)
136 clog << "HTTP server doesn't give Reason-Phrase for " << Result << std::endl;
137 }
138 else if (elements != 4)
139 return _error->Error(_("The HTTP server sent an invalid reply header"));
140 }
141 else
142 {
143 Major = 0;
144 Minor = 9;
145 if (sscanf(Line.c_str(),"HTTP %3u%359[^\n]",&Result,Code) != 2)
146 return _error->Error(_("The HTTP server sent an invalid reply header"));
147 }
148
149 /* Check the HTTP response header to get the default persistance
150 state. */
151 if (Major < 1)
152 Persistent = false;
153 else
154 {
155 if (Major == 1 && Minor == 0)
156 Persistent = false;
157 else
158 Persistent = true;
159 }
160
161 return true;
162 }
163
164 if (stringcasecmp(Tag,"Content-Length:") == 0)
165 {
166 if (Encoding == Closes)
167 Encoding = Stream;
168 HaveContent = true;
169
170 // The length is already set from the Content-Range header
171 if (StartPos != 0)
172 return true;
173
174 Size = strtoull(Val.c_str(), NULL, 10);
175 if (Size >= std::numeric_limits<unsigned long long>::max())
176 return _error->Errno("HeaderLine", _("The HTTP server sent an invalid Content-Length header"));
177 else if (Size == 0)
178 HaveContent = false;
179 return true;
180 }
181
182 if (stringcasecmp(Tag,"Content-Type:") == 0)
183 {
184 HaveContent = true;
185 return true;
186 }
187
188 if (stringcasecmp(Tag,"Content-Range:") == 0)
189 {
190 HaveContent = true;
191
192 // ยง14.16 says 'byte-range-resp-spec' should be a '*' in case of 416
193 if (Result == 416 && sscanf(Val.c_str(), "bytes */%llu",&Size) == 1)
194 {
195 StartPos = 1; // ignore Content-Length, it would override Size
196 HaveContent = false;
197 }
198 else if (sscanf(Val.c_str(),"bytes %llu-%*u/%llu",&StartPos,&Size) != 2)
199 return _error->Error(_("The HTTP server sent an invalid Content-Range header"));
200 if ((unsigned long long)StartPos > Size)
201 return _error->Error(_("This HTTP server has broken range support"));
202 return true;
203 }
204
205 if (stringcasecmp(Tag,"Transfer-Encoding:") == 0)
206 {
207 HaveContent = true;
208 if (stringcasecmp(Val,"chunked") == 0)
209 Encoding = Chunked;
210 return true;
211 }
212
213 if (stringcasecmp(Tag,"Connection:") == 0)
214 {
215 if (stringcasecmp(Val,"close") == 0)
216 Persistent = false;
217 if (stringcasecmp(Val,"keep-alive") == 0)
218 Persistent = true;
219 return true;
220 }
221
222 if (stringcasecmp(Tag,"Last-Modified:") == 0)
223 {
224 if (RFC1123StrToTime(Val.c_str(), Date) == false)
225 return _error->Error(_("Unknown date format"));
226 return true;
227 }
228
229 if (stringcasecmp(Tag,"Location:") == 0)
230 {
231 Location = Val;
232 return true;
233 }
234
235 return true;
236}
237 /*}}}*/
238// ServerState::ServerState - Constructor /*{{{*/
239ServerState::ServerState(URI Srv, ServerMethod *Owner) : ServerName(Srv), TimeOut(120), Owner(Owner)
240{
241 Reset();
242}
243 /*}}}*/
244
245bool ServerMethod::Configuration(string Message) /*{{{*/
246{
247 return pkgAcqMethod::Configuration(Message);
248}
249 /*}}}*/
250
251// ServerMethod::DealWithHeaders - Handle the retrieved header data /*{{{*/
252// ---------------------------------------------------------------------
253/* We look at the header data we got back from the server and decide what
254 to do. Returns DealWithHeadersResult (see http.h for details).
255 */
256ServerMethod::DealWithHeadersResult
257ServerMethod::DealWithHeaders(FetchResult &Res)
258{
259 // Not Modified
260 if (Server->Result == 304)
261 {
262 unlink(Queue->DestFile.c_str());
263 Res.IMSHit = true;
264 Res.LastModified = Queue->LastModified;
265 return IMS_HIT;
266 }
267
268 /* Redirect
269 *
270 * Note that it is only OK for us to treat all redirection the same
271 * because we *always* use GET, not other HTTP methods. There are
272 * three redirection codes for which it is not appropriate that we
273 * redirect. Pass on those codes so the error handling kicks in.
274 */
275 if (AllowRedirect
276 && (Server->Result > 300 && Server->Result < 400)
277 && (Server->Result != 300 // Multiple Choices
278 && Server->Result != 304 // Not Modified
279 && Server->Result != 306)) // (Not part of HTTP/1.1, reserved)
280 {
281 if (Server->Location.empty() == true);
282 else if (Server->Location[0] == '/' && Queue->Uri.empty() == false)
283 {
284 URI Uri = Queue->Uri;
285 if (Uri.Host.empty() == false)
286 NextURI = URI::SiteOnly(Uri);
287 else
288 NextURI.clear();
289 NextURI.append(DeQuoteString(Server->Location));
290 return TRY_AGAIN_OR_REDIRECT;
291 }
292 else
293 {
294 NextURI = DeQuoteString(Server->Location);
295 URI tmpURI = NextURI;
296 // Do not allow a redirection to switch protocol
297 if (tmpURI.Access == "http")
298 return TRY_AGAIN_OR_REDIRECT;
299 }
300 /* else pass through for error message */
301 }
302 // retry after an invalid range response without partial data
303 else if (Server->Result == 416)
304 {
305 struct stat SBuf;
306 if (stat(Queue->DestFile.c_str(),&SBuf) >= 0 && SBuf.st_size > 0)
307 {
308 if ((unsigned long long)SBuf.st_size == Server->Size)
309 {
310 // the file is completely downloaded, but was not moved
311 Server->StartPos = Server->Size;
312 Server->Result = 200;
313 Server->HaveContent = false;
314 }
315 else if (unlink(Queue->DestFile.c_str()) == 0)
316 {
317 NextURI = Queue->Uri;
318 return TRY_AGAIN_OR_REDIRECT;
319 }
320 }
321 }
322
323 /* We have a reply we dont handle. This should indicate a perm server
324 failure */
325 if (Server->Result < 200 || Server->Result >= 300)
326 {
327 char err[255];
328 snprintf(err,sizeof(err)-1,"HttpError%i",Server->Result);
329 SetFailReason(err);
330 _error->Error("%u %s",Server->Result,Server->Code);
331 if (Server->HaveContent == true)
332 return ERROR_WITH_CONTENT_PAGE;
333 return ERROR_UNRECOVERABLE;
334 }
335
336 // This is some sort of 2xx 'data follows' reply
337 Res.LastModified = Server->Date;
338 Res.Size = Server->Size;
339
340 // Open the file
341 delete File;
342 File = new FileFd(Queue->DestFile,FileFd::WriteAny);
343 if (_error->PendingError() == true)
344 return ERROR_NOT_FROM_SERVER;
345
346 FailFile = Queue->DestFile;
347 FailFile.c_str(); // Make sure we dont do a malloc in the signal handler
348 FailFd = File->Fd();
349 FailTime = Server->Date;
350
351 if (Server->InitHashes(*File) == false)
352 {
353 _error->Errno("read",_("Problem hashing file"));
354 return ERROR_NOT_FROM_SERVER;
355 }
356 if (Server->StartPos > 0)
357 Res.ResumePoint = Server->StartPos;
358
359 SetNonBlock(File->Fd(),true);
360 return FILE_IS_OPEN;
361}
362 /*}}}*/
363// ServerMethod::SigTerm - Handle a fatal signal /*{{{*/
364// ---------------------------------------------------------------------
365/* This closes and timestamps the open file. This is neccessary to get
366 resume behavoir on user abort */
367void ServerMethod::SigTerm(int)
368{
369 if (FailFd == -1)
370 _exit(100);
9ce3cfc9
DK
371
372 struct timespec times[2];
373 times[0].tv_sec = FailTime;
374 times[1].tv_sec = FailTime;
375 times[0].tv_nsec = times[1].tv_nsec = 0;
376 futimens(FailFd, times);
7330f4df 377 close(FailFd);
9ce3cfc9 378
7330f4df
DK
379 _exit(100);
380}
381 /*}}}*/
382// ServerMethod::Fetch - Fetch an item /*{{{*/
383// ---------------------------------------------------------------------
384/* This adds an item to the pipeline. We keep the pipeline at a fixed
385 depth. */
386bool ServerMethod::Fetch(FetchItem *)
387{
388 if (Server == 0)
389 return true;
390
391 // Queue the requests
392 int Depth = -1;
393 for (FetchItem *I = Queue; I != 0 && Depth < (signed)PipelineDepth;
394 I = I->Next, Depth++)
395 {
396 // If pipelining is disabled, we only queue 1 request
397 if (Server->Pipeline == false && Depth >= 0)
398 break;
399
400 // Make sure we stick with the same server
401 if (Server->Comp(I->Uri) == false)
402 break;
403 if (QueueBack == I)
404 {
405 QueueBack = I->Next;
406 SendReq(I);
407 continue;
408 }
409 }
410
411 return true;
412};
413 /*}}}*/
414// ServerMethod::Loop - Main loop /*{{{*/
415int ServerMethod::Loop()
416{
417 typedef vector<string> StringVector;
418 typedef vector<string>::iterator StringVectorIterator;
419 map<string, StringVector> Redirected;
420
421 signal(SIGTERM,SigTerm);
422 signal(SIGINT,SigTerm);
423
424 Server = 0;
425
426 int FailCounter = 0;
427 while (1)
428 {
429 // We have no commands, wait for some to arrive
430 if (Queue == 0)
431 {
432 if (WaitFd(STDIN_FILENO) == false)
433 return 0;
434 }
435
436 /* Run messages, we can accept 0 (no message) if we didn't
437 do a WaitFd above.. Otherwise the FD is closed. */
438 int Result = Run(true);
439 if (Result != -1 && (Result != 0 || Queue == 0))
440 {
441 if(FailReason.empty() == false ||
442 _config->FindB("Acquire::http::DependOnSTDIN", true) == true)
443 return 100;
444 else
445 return 0;
446 }
447
448 if (Queue == 0)
449 continue;
450
451 // Connect to the server
452 if (Server == 0 || Server->Comp(Queue->Uri) == false)
453 {
454 delete Server;
455 Server = CreateServerState(Queue->Uri);
456 }
457 /* If the server has explicitly said this is the last connection
458 then we pre-emptively shut down the pipeline and tear down
459 the connection. This will speed up HTTP/1.0 servers a tad
460 since we don't have to wait for the close sequence to
461 complete */
462 if (Server->Persistent == false)
463 Server->Close();
464
465 // Reset the pipeline
466 if (Server->IsOpen() == false)
467 QueueBack = Queue;
468
469 // Connnect to the host
470 if (Server->Open() == false)
471 {
472 Fail(true);
473 delete Server;
474 Server = 0;
475 continue;
476 }
477
478 // Fill the pipeline.
479 Fetch(0);
480
481 // Fetch the next URL header data from the server.
482 switch (Server->RunHeaders(File))
483 {
484 case ServerState::RUN_HEADERS_OK:
485 break;
486
487 // The header data is bad
488 case ServerState::RUN_HEADERS_PARSE_ERROR:
489 {
490 _error->Error(_("Bad header data"));
491 Fail(true);
492 RotateDNS();
493 continue;
494 }
495
496 // The server closed a connection during the header get..
497 default:
498 case ServerState::RUN_HEADERS_IO_ERROR:
499 {
500 FailCounter++;
501 _error->Discard();
502 Server->Close();
503 Server->Pipeline = false;
504
505 if (FailCounter >= 2)
506 {
507 Fail(_("Connection failed"),true);
508 FailCounter = 0;
509 }
510
511 RotateDNS();
512 continue;
513 }
514 };
515
516 // Decide what to do.
517 FetchResult Res;
518 Res.Filename = Queue->DestFile;
519 switch (DealWithHeaders(Res))
520 {
521 // Ok, the file is Open
522 case FILE_IS_OPEN:
523 {
524 URIStart(Res);
525
526 // Run the data
527 bool Result = true;
528 if (Server->HaveContent)
529 Result = Server->RunData(File);
530
531 /* If the server is sending back sizeless responses then fill in
532 the size now */
533 if (Res.Size == 0)
534 Res.Size = File->Size();
535
536 // Close the file, destroy the FD object and timestamp it
537 FailFd = -1;
538 delete File;
539 File = 0;
540
541 // Timestamp
9ce3cfc9
DK
542 struct timespec times[2];
543 times[0].tv_sec = times[1].tv_sec = Server->Date;
544 times[0].tv_nsec = times[1].tv_nsec = 0;
545 utimensat(AT_FDCWD, Queue->DestFile.c_str(), times, AT_SYMLINK_NOFOLLOW);
7330f4df
DK
546
547 // Send status to APT
548 if (Result == true)
549 {
550 Res.TakeHashes(*Server->GetHashes());
551 URIDone(Res);
552 }
553 else
554 {
555 if (Server->IsOpen() == false)
556 {
557 FailCounter++;
558 _error->Discard();
559 Server->Close();
560
561 if (FailCounter >= 2)
562 {
563 Fail(_("Connection failed"),true);
564 FailCounter = 0;
565 }
566
567 QueueBack = Queue;
568 }
569 else
570 Fail(true);
571 }
572 break;
573 }
574
575 // IMS hit
576 case IMS_HIT:
577 {
578 URIDone(Res);
579 break;
580 }
581
582 // Hard server error, not found or something
583 case ERROR_UNRECOVERABLE:
584 {
585 Fail();
586 break;
587 }
588
589 // Hard internal error, kill the connection and fail
590 case ERROR_NOT_FROM_SERVER:
591 {
592 delete File;
593 File = 0;
594
595 Fail();
596 RotateDNS();
597 Server->Close();
598 break;
599 }
600
601 // We need to flush the data, the header is like a 404 w/ error text
602 case ERROR_WITH_CONTENT_PAGE:
603 {
604 Fail();
605
606 // Send to content to dev/null
607 File = new FileFd("/dev/null",FileFd::WriteExists);
608 Server->RunData(File);
609 delete File;
610 File = 0;
611 break;
612 }
613
614 // Try again with a new URL
615 case TRY_AGAIN_OR_REDIRECT:
616 {
617 // Clear rest of response if there is content
618 if (Server->HaveContent)
619 {
620 File = new FileFd("/dev/null",FileFd::WriteExists);
621 Server->RunData(File);
622 delete File;
623 File = 0;
624 }
625
626 /* Detect redirect loops. No more redirects are allowed
627 after the same URI is seen twice in a queue item. */
628 StringVector &R = Redirected[Queue->DestFile];
629 bool StopRedirects = false;
630 if (R.empty() == true)
631 R.push_back(Queue->Uri);
632 else if (R[0] == "STOP" || R.size() > 10)
633 StopRedirects = true;
634 else
635 {
636 for (StringVectorIterator I = R.begin(); I != R.end(); ++I)
637 if (Queue->Uri == *I)
638 {
639 R[0] = "STOP";
640 break;
641 }
642
643 R.push_back(Queue->Uri);
644 }
645
646 if (StopRedirects == false)
647 Redirect(NextURI);
648 else
649 Fail();
650
651 break;
652 }
653
654 default:
655 Fail(_("Internal error"));
656 break;
657 }
658
659 FailCounter = 0;
660 }
661
662 return 0;
663}
664 /*}}}*/