Changeset 105 for trunk/data/apt

Show
Ignore:
Timestamp:
02/26/08 11:25:49 (11 months ago)
Author:
saurik
Message:

Added some better error checking to the new CFNetwork APT backend.

Files:
1 modified

Legend:

Unmodified
Added
Removed
  • trunk/data/apt/cfnetwork.diff

    r101 r105  
    11diff -ru apt-0.6.46.4.1/methods/http.cc apt-0.6.46.4.1+iPhone/methods/http.cc 
    22--- apt-0.6.46.4.1/methods/http.cc      2006-12-04 14:37:36.000000000 +0000 
    3 +++ apt-0.6.46.4.1+iPhone/methods/http.cc       2008-02-25 10:23:18.000000000 +0000 
     3+++ apt-0.6.46.4.1+iPhone/methods/http.cc       2008-02-26 05:07:59.000000000 +0000 
    44@@ -44,6 +48,9 @@ 
    55 // Internet stuff 
     
    1212 #include "rfc2553emu.h" 
    1313 #include "http.h" 
    14 @@ -1062,159 +1069,192 @@ 
     14@@ -1062,159 +1069,201 @@ 
    1515  
    1616       if (Queue == 0) 
     
    2222-        delete Server; 
    2323-        Server = new ServerState(Queue->Uri,this); 
    24 + 
    25 +      CFStringEncoding se = kCFStringEncodingUTF8; 
    26 + 
    27 +      CFStringRef sr = CFStringCreateWithCString(kCFAllocatorDefault, Queue->Uri.c_str(), se); 
    28 +      CFURLRef ur = CFURLCreateWithString(kCFAllocatorDefault, sr, NULL); 
    29 +      CFRelease(sr); 
    30 +      CFHTTPMessageRef hm = CFHTTPMessageCreateRequest(kCFAllocatorDefault, CFSTR("GET"), ur, kCFHTTPVersion1_1); 
    31 +      CFRelease(ur); 
    32 + 
    33 +      struct stat SBuf; 
    34 +      if (stat(Queue->DestFile.c_str(), &SBuf) >= 0 && SBuf.st_size > 0) { 
    35 +         sr = CFStringCreateWithFormat(kCFAllocatorDefault, NULL, CFSTR("bytes=%li-"), (long) SBuf.st_size - 1); 
    36 +         CFHTTPMessageSetHeaderFieldValue(hm, CFSTR("Range"), sr); 
    37 +         CFRelease(sr); 
    38 + 
    39 +         sr = CFStringCreateWithCString(kCFAllocatorDefault, TimeRFC1123(SBuf.st_mtime).c_str(), se); 
    40 +         CFHTTPMessageSetHeaderFieldValue(hm, CFSTR("If-Range"), sr); 
    41 +         CFRelease(sr); 
    42 +      } else if (Queue->LastModified != 0) { 
    43 +         sr = CFStringCreateWithCString(kCFAllocatorDefault, TimeRFC1123(SBuf.st_mtime).c_str(), se); 
    44 +         CFHTTPMessageSetHeaderFieldValue(hm, CFSTR("If-Modified-Since"), sr); 
    45 +         CFRelease(sr); 
    46        } 
     24-      } 
    4725-       
    4826-      /* If the server has explicitly said this is the last connection 
     
    6543-        Server = 0; 
    6644-        continue; 
    67 + 
    68 +      CFHTTPMessageSetHeaderFieldValue(hm, CFSTR("User-Agent"), CFSTR("Telesphoreo APT-HTTP/1.0.98")); 
    69 +      CFReadStreamRef rs = CFReadStreamCreateForHTTPRequest(kCFAllocatorDefault, hm); 
    70 +      CFRelease(hm); 
    71 + 
    72 +      CFReadStreamSetProperty(rs, kCFStreamPropertyHTTPShouldAutoredirect, kCFBooleanTrue); 
    73 +      CFReadStreamSetProperty(rs, kCFStreamPropertyHTTPAttemptPersistentConnection, kCFBooleanTrue); 
    74 + 
    75 +      URI uri = Queue->Uri; 
    76 + 
    77 +      Status("Connecting to %s", uri.Host.c_str()); 
    78 + 
    79 +      if (!CFReadStreamOpen(rs)) { 
    80 +         Fail(true); 
    81 +         continue; 
    82        } 
     45-      } 
    8346  
    8447-      // Fill the pipeline. 
     
    11982-        } 
    12083-      }; 
    121 +      uint8_t data[10240]; 
    122 +      CFIndex rd = CFReadStreamRead(rs, data, sizeof(data)); 
     84+      CFStringEncoding se = kCFStringEncodingUTF8; 
     85+ 
     86+      CFStringRef sr = CFStringCreateWithCString(kCFAllocatorDefault, Queue->Uri.c_str(), se); 
     87+      CFURLRef ur = CFURLCreateWithString(kCFAllocatorDefault, sr, NULL); 
     88+      CFRelease(sr); 
     89+      CFHTTPMessageRef hm = CFHTTPMessageCreateRequest(kCFAllocatorDefault, CFSTR("GET"), ur, kCFHTTPVersion1_1); 
     90+      CFRelease(ur); 
     91+ 
     92+      struct stat SBuf; 
     93+      if (stat(Queue->DestFile.c_str(), &SBuf) >= 0 && SBuf.st_size > 0) { 
     94+         sr = CFStringCreateWithFormat(kCFAllocatorDefault, NULL, CFSTR("bytes=%li-"), (long) SBuf.st_size - 1); 
     95+         CFHTTPMessageSetHeaderFieldValue(hm, CFSTR("Range"), sr); 
     96+         CFRelease(sr); 
     97+ 
     98+         sr = CFStringCreateWithCString(kCFAllocatorDefault, TimeRFC1123(SBuf.st_mtime).c_str(), se); 
     99+         CFHTTPMessageSetHeaderFieldValue(hm, CFSTR("If-Range"), sr); 
     100+         CFRelease(sr); 
     101+      } else if (Queue->LastModified != 0) { 
     102+         sr = CFStringCreateWithCString(kCFAllocatorDefault, TimeRFC1123(SBuf.st_mtime).c_str(), se); 
     103+         CFHTTPMessageSetHeaderFieldValue(hm, CFSTR("If-Modified-Since"), sr); 
     104+         CFRelease(sr); 
     105+      } 
     106+ 
     107+      CFHTTPMessageSetHeaderFieldValue(hm, CFSTR("User-Agent"), CFSTR("Telesphoreo APT-HTTP/1.0.98")); 
     108+      CFReadStreamRef rs = CFReadStreamCreateForHTTPRequest(kCFAllocatorDefault, hm); 
     109+      CFRelease(hm); 
     110+ 
     111+      CFReadStreamSetProperty(rs, kCFStreamPropertyHTTPShouldAutoredirect, kCFBooleanTrue); 
     112+      CFReadStreamSetProperty(rs, kCFStreamPropertyHTTPAttemptPersistentConnection, kCFBooleanTrue); 
     113+ 
     114+      URI uri = Queue->Uri; 
    123115  
    124116-      // Decide what to do. 
    125117       FetchResult Res; 
     118+ 
     119+      uint8_t data[10240]; 
     120+      size_t offset = 0; 
     121+ 
     122+      Status("Connecting to %s", uri.Host.c_str()); 
     123+ 
     124+      if (!CFReadStreamOpen(rs)) { 
     125+         _error->Error("Unable to open stream"); 
     126+         Fail(true); 
     127+         goto done; 
     128+      } 
     129+ 
     130+      CFIndex rd = CFReadStreamRead(rs, data, sizeof(data)); 
     131+ 
     132+      if (rd == -1) { 
     133+         _error->Error("Stream read failure"); 
     134+         Fail(true); 
     135+         goto done; 
     136+      } 
     137+ 
    126138       Res.Filename = Queue->DestFile; 
    127139-      switch (DealWithHeaders(Res,Server)) 
     
    139151-           /* If the server is sending back sizeless responses then fill in 
    140152-              the size now */ 
    141 +      size_t offset = 0; 
    142 + 
    143153+      sr = CFHTTPMessageCopyHeaderFieldValue(hm, CFSTR("Content-Range")); 
    144154+      if (sr != NULL) { 
     
    236246+        URIStart(Res); 
    237247+ 
    238 +         read: if (rd == -1) 
     248+         read: if (rd == -1) { 
     249+            _error->Error("Stream read failure"); 
    239250+            Fail(true); 
    240 +         else if (rd == 0) { 
     251+         } else if (rd == 0) { 
    241252            if (Res.Size == 0) 
    242253               Res.Size = File->Size(); 
     
    290301-           delete File; 
    291302-           File = 0; 
    292 +         } else { 
    293 +           hash.Add(data, rd); 
    294   
     303- 
    295304-           Fail(); 
    296305-           RotateDNS(); 
     
    298307-           break; 
    299308-        } 
    300 - 
     309+         } else { 
     310+           hash.Add(data, rd); 
     311  
    301312-        // We need to flush the data, the header is like a 404 w/ error text 
    302313-        case 4: 
     
    344355diff -ru apt-0.6.46.4.1/methods/makefile apt-0.6.46.4.1+iPhone/methods/makefile 
    345356--- apt-0.6.46.4.1/methods/makefile     2006-12-04 14:37:36.000000000 +0000 
    346 +++ apt-0.6.46.4.1+iPhone/methods/makefile      2008-02-25 10:18:24.000000000 +0000 
     357+++ apt-0.6.46.4.1+iPhone/methods/makefile      2008-02-25 10:24:23.000000000 +0000 
    347358@@ -47,7 +47,7 @@ 
    348359