OLD | NEW |
---|---|
(Empty) | |
1 // Copyright 2014 The Crashpad Authors. All rights reserved. | |
2 // | |
3 // Licensed under the Apache License, Version 2.0 (the "License"); | |
4 // you may not use this file except in compliance with the License. | |
5 // You may obtain a copy of the License at | |
6 // | |
7 // http://www.apache.org/licenses/LICENSE-2.0 | |
8 // | |
9 // Unless required by applicable law or agreed to in writing, software | |
10 // distributed under the License is distributed on an "AS IS" BASIS, | |
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
12 // See the License for the specific language governing permissions and | |
13 // limitations under the License. | |
14 | |
15 #include "util/net/http_transport.h" | |
16 | |
17 #import <Foundation/Foundation.h> | |
18 | |
19 #include "base/mac/foundation_util.h" | |
20 #import "base/mac/scoped_nsobject.h" | |
21 #include "base/strings/stringprintf.h" | |
22 #include "base/strings/sys_string_conversions.h" | |
23 #include "util/net/http_body.h" | |
24 | |
25 @interface CrashpadHTTPBodyStreamTransport : NSInputStream { | |
26 @private | |
27 NSStreamStatus streamStatus_; | |
28 id<NSStreamDelegate> delegate_; | |
29 crashpad::HTTPBodyStream* bodyStream_; // weak | |
30 } | |
31 - (instancetype)initWithBodyStream:(crashpad::HTTPBodyStream*)bodyStream; | |
32 @end | |
33 | |
34 @implementation CrashpadHTTPBodyStreamTransport | |
35 | |
36 - (instancetype)initWithBodyStream:(crashpad::HTTPBodyStream*)bodyStream { | |
37 if ((self = [super init])) { | |
38 streamStatus_ = NSStreamStatusNotOpen; | |
39 bodyStream_ = bodyStream; | |
40 } | |
41 return self; | |
42 } | |
43 | |
44 // NSInputStream: | |
45 | |
46 - (BOOL)hasBytesAvailable { | |
47 // Per Apple's documentation: "May also return YES if a read must be attempted | |
48 // in order to determine the availability of bytes." | |
49 switch (streamStatus_) { | |
50 case NSStreamStatusAtEnd: | |
51 case NSStreamStatusClosed: | |
52 case NSStreamStatusError: | |
53 return NO; | |
54 default: | |
55 return YES; | |
56 } | |
57 } | |
58 | |
59 - (NSInteger)read:(uint8_t*)buffer maxLength:(NSUInteger)maxLen { | |
60 streamStatus_ = NSStreamStatusReading; | |
61 | |
62 NSInteger rv = bodyStream_->GetBytesBuffer(buffer, maxLen); | |
63 | |
64 if (rv == 0) | |
65 streamStatus_ = NSStreamStatusAtEnd; | |
66 else if (rv < 0) | |
67 streamStatus_ = NSStreamStatusError; | |
68 else | |
69 streamStatus_ = NSStreamStatusOpen; | |
70 | |
71 return rv; | |
72 } | |
73 | |
74 - (BOOL)getBuffer:(uint8_t**)buffer length:(NSUInteger*)length { | |
75 return NO; | |
76 } | |
77 | |
78 // NSStream: | |
79 | |
80 - (void)scheduleInRunLoop:(NSRunLoop*)runLoop | |
81 forMode:(NSString*)mode { | |
82 } | |
83 | |
84 - (void)removeFromRunLoop:(NSRunLoop*)runLoop | |
85 forMode:(NSString*)mode { | |
86 } | |
87 | |
88 - (void)open { | |
89 streamStatus_ = NSStreamStatusOpen; | |
90 } | |
91 | |
92 - (void)close { | |
93 streamStatus_ = NSStreamStatusClosed; | |
94 } | |
95 | |
96 - (NSStreamStatus)streamStatus { | |
97 return streamStatus_; | |
98 } | |
99 | |
100 - (id<NSStreamDelegate>)delegate { | |
101 return delegate_; | |
102 } | |
103 | |
104 - (void)setDelegate:(id)delegate { | |
105 delegate_ = delegate; | |
106 } | |
107 | |
108 @end | |
109 | |
110 namespace crashpad { | |
111 | |
112 class HTTPTransportMac final : public HTTPTransport { | |
Mark Mentovai
2014/10/31 16:07:55
This should go into an inner anonymous namespace n
Robert Sesek
2014/10/31 16:17:25
Done.
| |
113 public: | |
114 HTTPTransportMac(); | |
115 ~HTTPTransportMac() override; | |
116 | |
117 bool ExecuteSynchronously() override; | |
118 | |
119 private: | |
120 DISALLOW_COPY_AND_ASSIGN(HTTPTransportMac); | |
121 }; | |
122 | |
123 // static | |
124 scoped_ptr<HTTPTransport> HTTPTransport::Create() { | |
125 return scoped_ptr<HTTPTransport>(new HTTPTransportMac()); | |
126 } | |
127 | |
128 HTTPTransportMac::HTTPTransportMac() : HTTPTransport() { | |
129 } | |
130 | |
131 HTTPTransportMac::~HTTPTransportMac() { | |
132 } | |
133 | |
134 bool HTTPTransportMac::ExecuteSynchronously() { | |
135 DCHECK(body_stream()); | |
136 | |
137 @autoreleasepool { | |
138 NSString* url_ns_string = base::SysUTF8ToNSString(url()); | |
139 NSURL* url = [NSURL URLWithString:url_ns_string]; | |
140 NSMutableURLRequest* request = | |
141 [NSMutableURLRequest requestWithURL:url | |
142 cachePolicy:NSURLRequestUseProtocolCachePolicy | |
143 timeoutInterval:timeout()]; | |
144 [request setHTTPMethod:base::SysUTF8ToNSString(method())]; | |
145 | |
146 for (const auto& pair : headers()) { | |
147 [request setValue:base::SysUTF8ToNSString(pair.second) | |
148 forHTTPHeaderField:base::SysUTF8ToNSString(pair.first)]; | |
149 } | |
150 | |
151 base::scoped_nsobject<CrashpadHTTPBodyStreamTransport> transport( | |
152 [[CrashpadHTTPBodyStreamTransport alloc] initWithBodyStream: | |
153 body_stream()]); | |
154 [request setHTTPBodyStream:transport.get()]; | |
155 | |
156 NSURLResponse* response = nil; | |
157 NSError* error = nil; | |
158 [NSURLConnection sendSynchronousRequest:request | |
159 returningResponse:&response | |
160 error:&error]; | |
161 | |
162 if (error) { | |
163 LOG(ERROR) << [[error localizedDescription] UTF8String] << " (" | |
164 << [[error domain] UTF8String] << " " << [error code] << ")"; | |
165 return false; | |
166 } | |
167 if (!response) { | |
168 LOG(ERROR) << "no response"; | |
169 return false; | |
170 } | |
171 NSHTTPURLResponse* http_response = | |
172 base::mac::ObjCCast<NSHTTPURLResponse>(response); | |
173 if (!http_response) { | |
174 LOG(ERROR) << "no http_response"; | |
175 return false; | |
176 } | |
177 NSInteger http_status = [http_response statusCode]; | |
178 if (http_status != 200) { | |
179 LOG(ERROR) << base::StringPrintf("HTTP status %ld", | |
180 static_cast<long>(http_status)); | |
181 return false; | |
182 } | |
183 | |
184 return true; | |
185 } | |
186 } | |
187 | |
188 } // namespace crashpad | |
OLD | NEW |