OLD | NEW |
---|---|
1 // Copyright 2014 The Crashpad Authors. All rights reserved. | 1 // Copyright 2014 The Crashpad Authors. All rights reserved. |
2 // | 2 // |
3 // Licensed under the Apache License, Version 2.0 (the "License"); | 3 // Licensed under the Apache License, Version 2.0 (the "License"); |
4 // you may not use this file except in compliance with 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 | 5 // You may obtain a copy of the License at |
6 // | 6 // |
7 // http://www.apache.org/licenses/LICENSE-2.0 | 7 // http://www.apache.org/licenses/LICENSE-2.0 |
8 // | 8 // |
9 // Unless required by applicable law or agreed to in writing, software | 9 // Unless required by applicable law or agreed to in writing, software |
10 // distributed under the License is distributed on an "AS IS" BASIS, | 10 // distributed under the License is distributed on an "AS IS" BASIS, |
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
12 // See the License for the specific language governing permissions and | 12 // See the License for the specific language governing permissions and |
13 // limitations under the License. | 13 // limitations under the License. |
14 | 14 |
15 #include "util/net/http_transport.h" | 15 #include "util/net/http_transport.h" |
16 | 16 |
17 #include <CoreFoundation/CoreFoundation.h> | |
17 #import <Foundation/Foundation.h> | 18 #import <Foundation/Foundation.h> |
18 | 19 |
19 #include "base/mac/foundation_util.h" | 20 #include "base/mac/foundation_util.h" |
20 #import "base/mac/scoped_nsobject.h" | 21 #import "base/mac/scoped_nsobject.h" |
21 #include "base/strings/stringprintf.h" | 22 #include "base/strings/stringprintf.h" |
22 #include "base/strings/sys_string_conversions.h" | 23 #include "base/strings/sys_string_conversions.h" |
23 #include "util/net/http_body.h" | 24 #include "util/net/http_body.h" |
24 | 25 #include "third_party/apple_cf/CFStreamAbstract.h" |
Mark Mentovai
2015/03/11 20:47:42
third_party < util
Robert Sesek
2015/03/11 20:53:57
Done.
| |
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 | 26 |
110 namespace crashpad { | 27 namespace crashpad { |
111 | 28 |
112 namespace { | 29 namespace { |
113 | 30 |
31 // An implementation of CFReadStream. This implements the V0 callback | |
32 // scheme. | |
33 class HTTPBodyStreamCFReadStream { | |
34 public: | |
35 explicit HTTPBodyStreamCFReadStream(HTTPBodyStream* body_stream) | |
36 : body_stream_(body_stream) { | |
37 } | |
38 | |
39 // Creates a new NSInputStream, which the caller owns. | |
40 NSInputStream* CreateInputStream() { | |
41 CFStreamClientContext context = { | |
Mark Mentovai
2015/03/11 20:47:42
Can these both be const?
Robert Sesek
2015/03/11 20:53:56
No, only the callbacks.
| |
42 .version = 0, | |
43 .info = this, | |
44 .retain = nullptr, | |
45 .release = nullptr, | |
46 .copyDescription = nullptr | |
47 }; | |
48 CFReadStreamCallBacksV0 callbacks = { | |
49 .version = 0, | |
50 .open = &Open, | |
51 .openCompleted = &OpenCompleted, | |
52 .read = &Read, | |
53 .getBuffer = &GetBuffer, | |
54 .canRead = &CanRead, | |
55 .close = &Close, | |
56 .copyProperty = &CopyProperty, | |
57 .schedule = &Schedule, | |
58 .unschedule = &Unschedule | |
59 }; | |
60 CFReadStreamRef read_stream = CFReadStreamCreate(NULL, | |
Mark Mentovai
2015/03/11 20:47:42
nullptr
Robert Sesek
2015/03/11 20:53:57
Done. Force of habit with CF.
| |
61 reinterpret_cast<const CFReadStreamCallBacks*>(&callbacks), &context); | |
62 return base::mac::CFToNSCast(read_stream); | |
63 } | |
64 | |
65 private: | |
66 static HTTPBodyStream* GetStream(void* info) { | |
67 return static_cast<HTTPBodyStreamCFReadStream*>(info)->body_stream_; | |
68 } | |
69 | |
70 static Boolean Open(CFReadStreamRef stream, | |
71 CFStreamError* error, | |
72 Boolean* open_complete, | |
73 void* info) { | |
74 *open_complete = TRUE; | |
75 return TRUE; | |
76 } | |
77 | |
78 static Boolean OpenCompleted(CFReadStreamRef stream, | |
79 CFStreamError* error, | |
80 void* info) { | |
81 return TRUE; | |
82 } | |
83 | |
84 static CFIndex Read(CFReadStreamRef stream, | |
85 UInt8* buffer, | |
86 CFIndex buffer_length, | |
87 CFStreamError* error, | |
88 Boolean* at_eof, | |
89 void* info) { | |
90 ssize_t bytes_read = GetStream(info)->GetBytesBuffer(buffer, buffer_length); | |
Mark Mentovai
2015/03/11 20:47:42
Paranoia: if buffer_length == 0, just return 0.
Robert Sesek
2015/03/11 20:53:57
Done.
| |
91 if (bytes_read == 0) { | |
92 *at_eof = TRUE; | |
93 } else if (bytes_read < 0) { | |
94 error->error = -1; | |
95 error->domain = kCFStreamErrorDomainCustom; | |
96 } | |
97 | |
98 return bytes_read; | |
99 } | |
100 | |
101 static const UInt8* GetBuffer(CFReadStreamRef stream, | |
102 CFIndex max_bytes_to_read, | |
103 CFIndex* num_bytes_read, | |
104 CFStreamError* error, | |
105 Boolean* at_eof, | |
106 void* info) { | |
107 return nullptr; | |
108 } | |
109 | |
110 static Boolean CanRead(CFReadStreamRef stream, void* info) { | |
111 return TRUE; | |
112 } | |
113 | |
114 static void Close(CFReadStreamRef stream, void* info) {} | |
115 | |
116 static CFTypeRef CopyProperty(CFReadStreamRef stream, | |
117 CFStringRef property_name, | |
118 void* info) { | |
119 return nullptr; | |
120 } | |
121 | |
122 static void Schedule(CFReadStreamRef stream, | |
123 CFRunLoopRef run_loop, | |
124 CFStringRef run_loop_mode, | |
125 void* info) {} | |
126 | |
127 static void Unschedule(CFReadStreamRef stream, | |
128 CFRunLoopRef run_loop, | |
129 CFStringRef run_loop_mode, | |
130 void* info) {} | |
131 | |
132 HTTPBodyStream* body_stream_; // weak | |
133 | |
134 DISALLOW_COPY_AND_ASSIGN(HTTPBodyStreamCFReadStream); | |
135 }; | |
136 | |
114 class HTTPTransportMac final : public HTTPTransport { | 137 class HTTPTransportMac final : public HTTPTransport { |
115 public: | 138 public: |
116 HTTPTransportMac(); | 139 HTTPTransportMac(); |
117 ~HTTPTransportMac() override; | 140 ~HTTPTransportMac() override; |
118 | 141 |
119 bool ExecuteSynchronously(std::string* response_body) override; | 142 bool ExecuteSynchronously(std::string* response_body) override; |
120 | 143 |
121 private: | 144 private: |
122 DISALLOW_COPY_AND_ASSIGN(HTTPTransportMac); | 145 DISALLOW_COPY_AND_ASSIGN(HTTPTransportMac); |
123 }; | 146 }; |
(...skipping 14 matching lines...) Expand all Loading... | |
138 [NSMutableURLRequest requestWithURL:url | 161 [NSMutableURLRequest requestWithURL:url |
139 cachePolicy:NSURLRequestUseProtocolCachePolicy | 162 cachePolicy:NSURLRequestUseProtocolCachePolicy |
140 timeoutInterval:timeout()]; | 163 timeoutInterval:timeout()]; |
141 [request setHTTPMethod:base::SysUTF8ToNSString(method())]; | 164 [request setHTTPMethod:base::SysUTF8ToNSString(method())]; |
142 | 165 |
143 for (const auto& pair : headers()) { | 166 for (const auto& pair : headers()) { |
144 [request setValue:base::SysUTF8ToNSString(pair.second) | 167 [request setValue:base::SysUTF8ToNSString(pair.second) |
145 forHTTPHeaderField:base::SysUTF8ToNSString(pair.first)]; | 168 forHTTPHeaderField:base::SysUTF8ToNSString(pair.first)]; |
146 } | 169 } |
147 | 170 |
148 base::scoped_nsobject<CrashpadHTTPBodyStreamTransport> transport( | 171 HTTPBodyStreamCFReadStream body_stream_cf(body_stream()); |
149 [[CrashpadHTTPBodyStreamTransport alloc] initWithBodyStream: | 172 base::scoped_nsobject<NSInputStream> input_stream( |
150 body_stream()]); | 173 body_stream_cf.CreateInputStream()); |
151 [request setHTTPBodyStream:transport.get()]; | 174 [request setHTTPBodyStream:input_stream.get()]; |
152 | 175 |
153 NSURLResponse* response = nil; | 176 NSURLResponse* response = nil; |
154 NSError* error = nil; | 177 NSError* error = nil; |
155 NSData* body = [NSURLConnection sendSynchronousRequest:request | 178 NSData* body = [NSURLConnection sendSynchronousRequest:request |
156 returningResponse:&response | 179 returningResponse:&response |
157 error:&error]; | 180 error:&error]; |
158 | 181 |
159 if (error) { | 182 if (error) { |
160 LOG(ERROR) << [[error localizedDescription] UTF8String] << " (" | 183 LOG(ERROR) << [[error localizedDescription] UTF8String] << " (" |
161 << [[error domain] UTF8String] << " " << [error code] << ")"; | 184 << [[error domain] UTF8String] << " " << [error code] << ")"; |
(...skipping 26 matching lines...) Expand all Loading... | |
188 } | 211 } |
189 | 212 |
190 } // namespace | 213 } // namespace |
191 | 214 |
192 // static | 215 // static |
193 scoped_ptr<HTTPTransport> HTTPTransport::Create() { | 216 scoped_ptr<HTTPTransport> HTTPTransport::Create() { |
194 return scoped_ptr<HTTPTransport>(new HTTPTransportMac()); | 217 return scoped_ptr<HTTPTransport>(new HTTPTransportMac()); |
195 } | 218 } |
196 | 219 |
197 } // namespace crashpad | 220 } // namespace crashpad |
OLD | NEW |