What's new? | Help | Directory | Sign in
Google
  
  
  
    
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
// Copyright 2007, Google Inc.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are met:
//
// 1. Redistributions of source code must retain the above copyright notice,
// this list of conditions and the following disclaimer.
// 2. Redistributions in binary form must reproduce the above copyright notice,
// this list of conditions and the following disclaimer in the documentation
// and/or other materials provided with the distribution.
// 3. Neither the name of Google Inc. nor the names of its contributors may be
// used to endorse or promote products derived from this software without
// specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
// WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
// MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
// EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
// PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
// OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
// WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
// OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
// ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

#import "gears/base/common/string_utils.h"
#import "gears/base/common/http_utils.h"
#import "gears/base/safari/nsstring_utils.h"
#import "gears/localserver/common/http_constants.h"
#import "gears/localserver/common/localserver_db.h"
#import "gears/localserver/safari/localserver_db_proxy.h"

@implementation GearsWebCacheDB
//------------------------------------------------------------------------------
+ (BOOL)canService:(NSURLRequest *)request {
static NSString *bypass_localserver_header_name =
(NSString *)CFStringCreateWithString16(
HttpConstants::kXGoogleGearsBypassLocalServer);

// Check if the bypasslocalserver header field exists.
if ([request respondsToSelector:@selector(valueForHTTPHeaderField:)]) {
if ([request valueForHTTPHeaderField:bypass_localserver_header_name]
!= nil) {
return NO;
}
}

NSURL *url = [request URL];
NSString *urlStr = [url absoluteString];
std::string16 url16;

if ([urlStr string16:&url16]) {
WebCacheDB *db = WebCacheDB::GetDB();

if (db && db->CanService(url16.c_str(), NULL))
return YES;
}

return NO;
}

//------------------------------------------------------------------------------
+ (NSData *)service:(NSURL *)url mimeType:(NSString **)mimeType
headers:(NSDictionary **)headers statusCode:(int *)statusCode
redirectURL:(NSURL **)redirectURL {
std::string16 url16;

assert(url);
assert(mimeType);
assert(headers);
assert(statusCode);
assert(redirectURL);

if (![[url absoluteString] string16:&url16])
return nil;

WebCacheDB *db = WebCacheDB::GetDB();
WebCacheDB::PayloadInfo payload;

if (!db->Service(url16.c_str(), NULL, false, &payload))
return nil;

// The requested url may redirect to another location
std::string16 redirect_url;
if (payload.IsHttpRedirect()) {
// We collapse a chain of redirects and hop directly to the final
// location for which we have a cache entry.
int num_redirects = 0;
while (payload.IsHttpRedirect()) {
if (!payload.GetHeader(HttpConstants::kLocationHeader, &redirect_url))
return nil;

// Fetch a response for redirect_url from our DB
if (!db->Service(redirect_url.c_str(), NULL, false, &payload))
return nil;

// Make sure we don't get stuck in an infinite redirect loop.
++num_redirects;
if (num_redirects > HttpConstants::kMaxRedirects) {
LOG16((STRING16(L"Redirect chain too long %s -> %s"),
url16.c_str(), redirect_url.c_str()));
return nil;
}
}

*redirectURL = [NSURL URLWithString:
[NSString stringWithString16:redirect_url.c_str()]];
} else {
*redirectURL = nil;
}

std::string16 header16;

if (payload.GetHeader(HttpConstants::kXGearsSafariCapturedMimeType,
&header16))
*mimeType = [NSString stringWithString16:header16.c_str()];
else
*mimeType = nil;

*statusCode = payload.status_code;
NSMutableDictionary *ret_headers = nil;

// Crack header string and convert to NSDictionary.
// TODO(playmobil): If this turns out to be a performance bottleneck, we may
// want to just store the cracked header in the payload rather than re-parsing
// them on every request.
const std::string16 &headers_utf16 = payload.headers;
if (!headers_utf16.empty()) {
std::string headers_utf8;
if (!String16ToUTF8(headers_utf16.c_str(),
headers_utf16.length(),
&headers_utf8)) {
return nil;
}

HTTPHeaders parsed_headers;
const char *body = headers_utf8.c_str();
uint32 bodylen = headers_utf8.length();
if (!HTTPUtils::ParseHTTPHeaders(&body, &bodylen, &parsed_headers, true)) {
return nil;
}
ret_headers = [[[NSMutableDictionary alloc] init] autorelease];
for (HTTPHeaders::const_iterator it = parsed_headers.begin();
it != parsed_headers.end();
++it) {
NSString *header_name = [NSString stringWithUTF8String:it->first];
NSString *header_value = [NSString stringWithUTF8String:it->second];
[ret_headers setValue:header_value forKey:header_name];
}
}
*headers = ret_headers;

// Copy the data
std::vector<unsigned char> *dataVec = payload.data.get();
const unsigned char *bytes = dataVec ? &(*dataVec)[0] : NULL;
unsigned int length = dataVec ? dataVec->size() : 0;

return [NSData dataWithBytes:bytes length:length];
}

@end

Show details Hide details

Change log

r2364 by gears.daemon on Jul 15, 2008   Diff
[Author: playmobil]

* #include -> #import in all obj-c files.
* Added include guards to all obj-c
headers.
 * Removed a few stale Safari files.

PRESUBMIT=passed
R=dimich
CC=gears-eng@googlegroups.com
DELTA=339  (66 added, 171 deleted, 102
changed)
...
Go to: 
Project members, sign in to write a code review

Older revisions

r2265 by gears.daemon on Jul 07, 2008   Diff
[Author: playmobil]

Bail on an infinite redirect loop,
this CL doesn't fix the root cause for
" issue 569 : Safari: Docs offline won't
...
r2112 by gears.daemon on Jun 25, 2008   Diff
[Author: steveblock]

Moves CFStringRefToString16 to new
OSX-specific string utils file.

...
r1814 by gears.daemon on May 30, 2008   Diff
[Author: mpcomplete]

Add BrowsingContext to associate with
network requests.

...
All revisions of this file

File info

Size: 5987 bytes, 162 lines