reorder includes: add <config.h> if needed and include it at first
[ntk/apt.git] / methods / http.cc
index 2dae87a..e505b81 100644 (file)
@@ -25,6 +25,8 @@
    ##################################################################### */
                                                                        /*}}}*/
 // Include Files                                                       /*{{{*/
+#include <config.h>
+
 #include <apt-pkg/fileutl.h>
 #include <apt-pkg/acquire-method.h>
 #include <apt-pkg/error.h>
@@ -41,8 +43,6 @@
 #include <string.h>
 #include <iostream>
 #include <map>
-#include <apti18n.h>
-
 
 // Internet stuff
 #include <netdb.h>
@@ -51,6 +51,8 @@
 #include "connect.h"
 #include "rfc2553emu.h"
 #include "http.h"
+
+#include <apti18n.h>
                                                                        /*}}}*/
 using namespace std;
 
@@ -67,7 +69,7 @@ unsigned long CircleBuf::BwReadLimit=0;
 unsigned long CircleBuf::BwTickReadData=0;
 struct timeval CircleBuf::BwReadTick={0,0};
 const unsigned int CircleBuf::BW_HZ=10;
-  
 // CircleBuf::CircleBuf - Circular input buffer                                /*{{{*/
 // ---------------------------------------------------------------------
 /* */
@@ -378,7 +380,7 @@ bool ServerState::Close()
 // ---------------------------------------------------------------------
 /* Returns 0 if things are OK, 1 if an IO error occurred and 2 if a header
    parse error occurred */
-int ServerState::RunHeaders()
+ServerState::RunHeadersResult ServerState::RunHeaders()
 {
    State = Header;
    
@@ -407,7 +409,7 @@ int ServerState::RunHeaders()
         string::const_iterator J = I;
         for (; J != Data.end() && *J != '\n' && *J != '\r';J++);
         if (HeaderLine(string(I,J)) == false)
-           return 2;
+           return RUN_HEADERS_PARSE_ERROR;
         I = J;
       }
 
@@ -419,11 +421,11 @@ int ServerState::RunHeaders()
       if (Encoding == Closes && HaveContent == true)
         Persistent = false;
       
-      return 0;
+      return RUN_HEADERS_OK;
    }
    while (Owner->Go(false,this) == true);
    
-   return 1;
+   return RUN_HEADERS_IO_ERROR;
 }
                                                                        /*}}}*/
 // ServerState::RunData - Transfer the data from the socket            /*{{{*/
@@ -553,8 +555,14 @@ bool ServerState::HeaderLine(string Line)
       // Evil servers return no version
       if (Line[4] == '/')
       {
-        if (sscanf(Line.c_str(),"HTTP/%u.%u %u%[^\n]",&Major,&Minor,
-                   &Result,Code) != 4)
+        int const elements = sscanf(Line.c_str(),"HTTP/%u.%u %u%[^\n]",&Major,&Minor,&Result,Code);
+        if (elements == 3)
+        {
+           Code[0] = '\0';
+           if (Debug == true)
+              clog << "HTTP server doesn't give Reason-Phrase for " << Result << std::endl;
+        }
+        else if (elements != 4)
            return _error->Error(_("The HTTP server sent an invalid reply header"));
       }
       else
@@ -631,7 +639,7 @@ bool ServerState::HeaderLine(string Line)
    
    if (stringcasecmp(Tag,"Last-Modified:") == 0)
    {
-      if (StrToTime(Val,Date) == false)
+      if (RFC1123StrToTime(Val.c_str(), Date) == false)
         return _error->Error(_("Unknown date format"));
       return true;
    }
@@ -682,23 +690,25 @@ void HttpMethod::SendReq(FetchItem *Itm,CircleBuf &Out)
                 and a no-store directive for archives. */
       sprintf(Buf,"GET %s HTTP/1.1\r\nHost: %s\r\n",
              Itm->Uri.c_str(),ProperHost.c_str());
-      // only generate a cache control header if we actually want to 
-      // use a cache
-      if (_config->FindB("Acquire::http::No-Cache",false) == false)
+   }
+   // generate a cache control header (if needed)
+   if (_config->FindB("Acquire::http::No-Cache",false) == true) 
+   {
+      strcat(Buf,"Cache-Control: no-cache\r\nPragma: no-cache\r\n");
+   }
+   else
+   {
+      if (Itm->IndexFile == true) 
       {
-        if (Itm->IndexFile == true)
-           sprintf(Buf+strlen(Buf),"Cache-Control: max-age=%u\r\n",
-                   _config->FindI("Acquire::http::Max-Age",0));
-        else
-        {
-           if (_config->FindB("Acquire::http::No-Store",false) == true)
-              strcat(Buf,"Cache-Control: no-store\r\n");
-        }       
+        sprintf(Buf+strlen(Buf),"Cache-Control: max-age=%u\r\n",
+                _config->FindI("Acquire::http::Max-Age",0));
+      }
+      else
+      {
+        if (_config->FindB("Acquire::http::No-Store",false) == true)
+           strcat(Buf,"Cache-Control: no-store\r\n");
       }
    }
-   // generate a no-cache header if needed
-   if (_config->FindB("Acquire::http::No-Cache",false) == true)
-      strcat(Buf,"Cache-Control: no-cache\r\nPragma: no-cache\r\n");
 
    
    string Req = Buf;
@@ -770,9 +780,10 @@ bool HttpMethod::Go(bool ToFile,ServerState *Srv)
    
    if (Srv->In.WriteSpace() == true && ToFile == true && FileFD != -1)
       FD_SET(FileFD,&wfds);
-   
+
    // Add stdin
-   FD_SET(STDIN_FILENO,&rfds);
+   if (_config->FindB("Acquire::http::DependOnSTDIN", true) == true)
+      FD_SET(STDIN_FILENO,&rfds);
          
    // Figure out the max fd
    int MaxFd = FileFD;
@@ -912,15 +923,10 @@ bool HttpMethod::ServerDie(ServerState *Srv)
 // HttpMethod::DealWithHeaders - Handle the retrieved header data      /*{{{*/
 // ---------------------------------------------------------------------
 /* We look at the header data we got back from the server and decide what
-   to do. Returns 
-     0 - File is open,
-     1 - IMS hit
-     3 - Unrecoverable error 
-     4 - Error with error content page
-     5 - Unrecoverable non-server error (close the connection) 
-     6 - Try again with a new or changed URI
+   to do. Returns DealWithHeadersResult (see http.h for details).
  */
-int HttpMethod::DealWithHeaders(FetchResult &Res,ServerState *Srv)
+HttpMethod::DealWithHeadersResult
+HttpMethod::DealWithHeaders(FetchResult &Res,ServerState *Srv)
 {
    // Not Modified
    if (Srv->Result == 304)
@@ -928,7 +934,7 @@ int HttpMethod::DealWithHeaders(FetchResult &Res,ServerState *Srv)
       unlink(Queue->DestFile.c_str());
       Res.IMSHit = true;
       Res.LastModified = Queue->LastModified;
-      return 1;
+      return IMS_HIT;
    }
    
    /* Redirect
@@ -944,10 +950,26 @@ int HttpMethod::DealWithHeaders(FetchResult &Res,ServerState *Srv)
            && Srv->Result != 304    // Not Modified
            && Srv->Result != 306))  // (Not part of HTTP/1.1, reserved)
    {
-      if (!Srv->Location.empty())
+      if (Srv->Location.empty() == true);
+      else if (Srv->Location[0] == '/' && Queue->Uri.empty() == false)
       {
-         NextURI = Srv->Location;
-         return 6;
+        URI Uri = Queue->Uri;
+        if (Uri.Host.empty() == false)
+        {
+           if (Uri.Port != 0)
+              strprintf(NextURI, "http://%s:%u", Uri.Host.c_str(), Uri.Port);
+           else
+              NextURI = "http://" + Uri.Host;
+        }
+        else
+           NextURI.clear();
+        NextURI.append(DeQuoteString(Srv->Location));
+        return TRY_AGAIN_OR_REDIRECT;
+      }
+      else
+      {
+         NextURI = DeQuoteString(Srv->Location);
+         return TRY_AGAIN_OR_REDIRECT;
       }
       /* else pass through for error message */
    }
@@ -956,10 +978,13 @@ int HttpMethod::DealWithHeaders(FetchResult &Res,ServerState *Srv)
       failure */
    if (Srv->Result < 200 || Srv->Result >= 300)
    {
+      char err[255];
+      snprintf(err,sizeof(err)-1,"HttpError%i",Srv->Result);
+      SetFailReason(err);
       _error->Error("%u %s",Srv->Result,Srv->Code);
       if (Srv->HaveContent == true)
-        return 4;
-      return 3;
+        return ERROR_WITH_CONTENT_PAGE;
+      return ERROR_UNRECOVERABLE;
    }
 
    // This is some sort of 2xx 'data follows' reply
@@ -970,7 +995,7 @@ int HttpMethod::DealWithHeaders(FetchResult &Res,ServerState *Srv)
    delete File;
    File = new FileFd(Queue->DestFile,FileFd::WriteAny);
    if (_error->PendingError() == true)
-      return 5;
+      return ERROR_NOT_FROM_SERVER;
 
    FailFile = Queue->DestFile;
    FailFile.c_str();   // Make sure we dont do a malloc in the signal handler
@@ -998,13 +1023,13 @@ int HttpMethod::DealWithHeaders(FetchResult &Res,ServerState *Srv)
       if (Srv->In.Hash->AddFD(File->Fd(),Srv->StartPos) == false)
       {
         _error->Errno("read",_("Problem hashing file"));
-        return 5;
+        return ERROR_NOT_FROM_SERVER;
       }
       lseek(File->Fd(),0,SEEK_END);
    }
    
    SetNonBlock(File->Fd(),true);
-   return 0;
+   return FILE_IS_OPEN;
 }
                                                                        /*}}}*/
 // HttpMethod::SigTerm - Handle a fatal signal                         /*{{{*/
@@ -1071,7 +1096,11 @@ bool HttpMethod::Configuration(string Message)
    PipelineDepth = _config->FindI("Acquire::http::Pipeline-Depth",
                                  PipelineDepth);
    Debug = _config->FindB("Debug::Acquire::http",false);
-   
+   AutoDetectProxyCmd = _config->Find("Acquire::http::ProxyAutoDetect");
+
+   // Get the proxy to use
+   AutoDetectProxy();
+
    return true;
 }
                                                                        /*}}}*/
@@ -1103,7 +1132,13 @@ int HttpMethod::Loop()
          do a WaitFd above.. Otherwise the FD is closed. */
       int Result = Run(true);
       if (Result != -1 && (Result != 0 || Queue == 0))
-        return 100;
+      {
+        if(FailReason.empty() == false ||
+           _config->FindB("Acquire::http::DependOnSTDIN", true) == true)
+           return 100;
+        else
+           return 0;
+      }
 
       if (Queue == 0)
         continue;
@@ -1141,11 +1176,11 @@ int HttpMethod::Loop()
       // Fetch the next URL header data from the server.
       switch (Server->RunHeaders())
       {
-        case 0:
+        case ServerState::RUN_HEADERS_OK:
         break;
         
         // The header data is bad
-        case 2:
+        case ServerState::RUN_HEADERS_PARSE_ERROR:
         {
            _error->Error(_("Bad header data"));
            Fail(true);
@@ -1155,7 +1190,7 @@ int HttpMethod::Loop()
         
         // The server closed a connection during the header get..
         default:
-        case 1:
+        case ServerState::RUN_HEADERS_IO_ERROR:
         {
            FailCounter++;
            _error->Discard();
@@ -1179,7 +1214,7 @@ int HttpMethod::Loop()
       switch (DealWithHeaders(Res,Server))
       {
         // Ok, the file is Open
-        case 0:
+        case FILE_IS_OPEN:
         {
            URIStart(Res);
 
@@ -1232,21 +1267,21 @@ int HttpMethod::Loop()
         }
         
         // IMS hit
-        case 1:
+        case IMS_HIT:
         {
            URIDone(Res);
            break;
         }
         
         // Hard server error, not found or something
-        case 3:
+        case ERROR_UNRECOVERABLE:
         {
            Fail();
            break;
         }
          
         // Hard internal error, kill the connection and fail
-        case 5:
+        case ERROR_NOT_FROM_SERVER:
         {
            delete File;
            File = 0;
@@ -1258,7 +1293,7 @@ int HttpMethod::Loop()
         }
 
         // We need to flush the data, the header is like a 404 w/ error text
-        case 4:
+        case ERROR_WITH_CONTENT_PAGE:
         {
            Fail();
            
@@ -1271,7 +1306,7 @@ int HttpMethod::Loop()
         }
         
          // Try again with a new URL
-         case 6:
+         case TRY_AGAIN_OR_REDIRECT:
          {
             // Clear rest of response if there is content
             if (Server->HaveContent)
@@ -1321,16 +1356,57 @@ int HttpMethod::Loop()
    return 0;
 }
                                                                        /*}}}*/
-
-int main()
+// HttpMethod::AutoDetectProxy - auto detect proxy                     /*{{{*/
+// ---------------------------------------------------------------------
+/* */
+bool HttpMethod::AutoDetectProxy()
 {
-   setlocale(LC_ALL, "");
-   // ignore SIGPIPE, this can happen on write() if the socket
-   // closes the connection (this is dealt with via ServerDie())
-   signal(SIGPIPE, SIG_IGN);
+   if (AutoDetectProxyCmd.empty())
+      return true;
+
+   if (Debug)
+      clog << "Using auto proxy detect command: " << AutoDetectProxyCmd << endl;
+
+   int Pipes[2] = {-1,-1};
+   if (pipe(Pipes) != 0)
+      return _error->Errno("pipe", "Failed to create Pipe");
+
+   pid_t Process = ExecFork();
+   if (Process == 0)
+   {
+      close(Pipes[0]);
+      dup2(Pipes[1],STDOUT_FILENO);
+      SetCloseExec(STDOUT_FILENO,false);
+
+      const char *Args[2];
+      Args[0] = AutoDetectProxyCmd.c_str();
+      Args[1] = 0;
+      execv(Args[0],(char **)Args);
+      cerr << "Failed to exec method " << Args[0] << endl;
+      _exit(100);
+   }
+   char buf[512];
+   int InFd = Pipes[0];
+   close(Pipes[1]);
+   int res = read(InFd, buf, sizeof(buf));
+   ExecWait(Process, "ProxyAutoDetect", true);
 
-   HttpMethod Mth;
-   return Mth.Loop();
+   if (res < 0)
+      return _error->Errno("read", "Failed to read");
+   if (res == 0)
+      return _error->Warning("ProxyAutoDetect returned no data");
+
+   // add trailing \0
+   buf[res] = 0;
+
+   if (Debug)
+      clog << "auto detect command returned: '" << buf << "'" << endl;
+
+   if (strstr(buf, "http://") == buf)
+      _config->Set("Acquire::http::proxy", _strstrip(buf));
+
+   return true;
 }
+                                                                       /*}}}*/