Merge remote-tracking branch 'vnwildman/debian/sid' into debian/sid
[ntk/apt.git] / methods / server.cc
index a212844..92d94e6 100644 (file)
 // Include Files                                                       /*{{{*/
 #include <config.h>
 
-#include <apt-pkg/fileutl.h>
 #include <apt-pkg/acquire-method.h>
 #include <apt-pkg/configuration.h>
 #include <apt-pkg/error.h>
-#include <apt-pkg/hashes.h>
-#include <apt-pkg/netrc.h>
+#include <apt-pkg/fileutl.h>
+#include <apt-pkg/strutl.h>
 
+#include <ctype.h>
+#include <signal.h>
+#include <stdio.h>
+#include <stdlib.h>
 #include <sys/stat.h>
 #include <sys/time.h>
-#include <utime.h>
+#include <time.h>
 #include <unistd.h>
-#include <signal.h>
-#include <stdio.h>
-#include <errno.h>
-#include <string.h>
-#include <climits>
 #include <iostream>
+#include <limits>
 #include <map>
+#include <string>
+#include <vector>
 
-// Internet stuff
-#include <netdb.h>
-
-#include "config.h"
-#include "connect.h"
-#include "rfc2553emu.h"
-#include "http.h"
+#include "server.h"
 
 #include <apti18n.h>
                                                                        /*}}}*/
@@ -49,7 +44,8 @@ time_t ServerMethod::FailTime = 0;
 // ---------------------------------------------------------------------
 /* Returns 0 if things are OK, 1 if an IO error occurred and 2 if a header
    parse error occurred */
-ServerState::RunHeadersResult ServerState::RunHeaders(FileFd * const File)
+ServerState::RunHeadersResult ServerState::RunHeaders(FileFd * const File,
+                                                      const std::string &Uri)
 {
    State = Header;
    
@@ -71,7 +67,7 @@ ServerState::RunHeadersResult ServerState::RunHeaders(FileFd * const File)
         continue;
 
       if (Owner->Debug == true)
-        clog << Data;
+        clog << "Answer for: " << Uri << endl << Data;
       
       for (string::const_iterator I = Data.begin(); I < Data.end(); ++I)
       {
@@ -86,7 +82,7 @@ ServerState::RunHeadersResult ServerState::RunHeaders(FileFd * const File)
       if (Result == 100)
         continue;
       
-      // Tidy up the connection persistance state.
+      // Tidy up the connection persistence state.
       if (Encoding == Closes && HaveContent == true)
         Persistent = false;
       
@@ -119,10 +115,10 @@ bool ServerState::HeaderLine(string Line)
    string::size_type Pos2 = Pos;
    while (Pos2 < Line.length() && isspace(Line[Pos2]) != 0)
       Pos2++;
-      
+
    string Tag = string(Line,0,Pos);
    string Val = string(Line,Pos2);
-   
+
    if (stringcasecmp(Tag.c_str(),Tag.c_str()+4,"HTTP") == 0)
    {
       // Evil servers return no version
@@ -146,7 +142,7 @@ bool ServerState::HeaderLine(string Line)
            return _error->Error(_("The HTTP server sent an invalid reply header"));
       }
 
-      /* Check the HTTP response header to get the default persistance
+      /* Check the HTTP response header to get the default persistence
          state. */
       if (Major < 1)
         Persistent = false;
@@ -159,14 +155,14 @@ bool ServerState::HeaderLine(string Line)
       }
 
       return true;
-   }      
-      
+   }
+
    if (stringcasecmp(Tag,"Content-Length:") == 0)
    {
       if (Encoding == Closes)
         Encoding = Stream;
       HaveContent = true;
-      
+
       // The length is already set from the Content-Range header
       if (StartPos != 0)
         return true;
@@ -184,7 +180,7 @@ bool ServerState::HeaderLine(string Line)
       HaveContent = true;
       return true;
    }
-   
+
    if (stringcasecmp(Tag,"Content-Range:") == 0)
    {
       HaveContent = true;
@@ -201,12 +197,12 @@ bool ServerState::HeaderLine(string Line)
         return _error->Error(_("This HTTP server has broken range support"));
       return true;
    }
-   
+
    if (stringcasecmp(Tag,"Transfer-Encoding:") == 0)
    {
       HaveContent = true;
       if (stringcasecmp(Val,"chunked") == 0)
-        Encoding = Chunked;      
+        Encoding = Chunked;
       return true;
    }
 
@@ -218,7 +214,7 @@ bool ServerState::HeaderLine(string Line)
         Persistent = true;
       return true;
    }
-   
+
    if (stringcasecmp(Tag,"Last-Modified:") == 0)
    {
       if (RFC1123StrToTime(Val.c_str(), Date) == false)
@@ -291,11 +287,15 @@ ServerMethod::DealWithHeaders(FetchResult &Res)
       }
       else
       {
-         NextURI = DeQuoteString(Server->Location);
-         URI tmpURI = NextURI;
-         // Do not allow a redirection to switch protocol
-         if (tmpURI.Access == "http")
-            return TRY_AGAIN_OR_REDIRECT;
+        NextURI = DeQuoteString(Server->Location);
+        URI tmpURI = NextURI;
+        URI Uri = Queue->Uri;
+        // same protocol redirects are okay
+        if (tmpURI.Access == Uri.Access)
+           return TRY_AGAIN_OR_REDIRECT;
+        // as well as http to https
+        else if (Uri.Access == "http" && tmpURI.Access == "https")
+           return TRY_AGAIN_OR_REDIRECT;
       }
       /* else pass through for error message */
    }
@@ -362,20 +362,20 @@ ServerMethod::DealWithHeaders(FetchResult &Res)
                                                                        /*}}}*/
 // ServerMethod::SigTerm - Handle a fatal signal                       /*{{{*/
 // ---------------------------------------------------------------------
-/* This closes and timestamps the open file. This is neccessary to get
+/* This closes and timestamps the open file. This is necessary to get
    resume behavoir on user abort */
 void ServerMethod::SigTerm(int)
 {
    if (FailFd == -1)
       _exit(100);
+
+   struct timeval times[2];
+   times[0].tv_sec = FailTime;
+   times[1].tv_sec = FailTime;
+   times[0].tv_usec = times[1].tv_usec = 0;
+   utimes(FailFile.c_str(), times);
    close(FailFd);
-   
-   // Timestamp
-   struct utimbuf UBuf;
-   UBuf.actime = FailTime;
-   UBuf.modtime = FailTime;
-   utime(FailFile.c_str(),&UBuf);
-   
+
    _exit(100);
 }
                                                                        /*}}}*/
@@ -409,7 +409,7 @@ bool ServerMethod::Fetch(FetchItem *)
    }
    
    return true;
-};
+}
                                                                        /*}}}*/
 // ServerMethod::Loop - Main loop                                      /*{{{*/
 int ServerMethod::Loop()
@@ -479,7 +479,7 @@ int ServerMethod::Loop()
       Fetch(0);
       
       // Fetch the next URL header data from the server.
-      switch (Server->RunHeaders(File))
+      switch (Server->RunHeaders(File, Queue->Uri))
       {
         case ServerState::RUN_HEADERS_OK:
         break;
@@ -539,11 +539,10 @@ int ServerMethod::Loop()
            File = 0;
            
            // Timestamp
-           struct utimbuf UBuf;
-           time(&UBuf.actime);
-           UBuf.actime = Server->Date;
-           UBuf.modtime = Server->Date;
-           utime(Queue->DestFile.c_str(),&UBuf);
+           struct timeval times[2];
+           times[0].tv_sec = times[1].tv_sec = Server->Date;
+           times[0].tv_usec = times[1].tv_usec = 0;
+           utimes(Queue->DestFile.c_str(), times);
 
            // Send status to APT
            if (Result == true)