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_mac.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_; | |
Mark Mentovai
2014/10/30 21:13:54
// weak
Robert Sesek
2014/10/31 14:48:14
Done.
| |
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 return YES; | |
Mark Mentovai
2014/10/30 21:13:54
I guess you could return NO if streamStatus_ is NS
Robert Sesek
2014/10/31 14:48:15
Done.
| |
50 } | |
51 | |
52 - (NSInteger)read:(uint8_t*)buffer maxLength:(NSUInteger)maxLen { | |
53 NSInteger rv = bodyStream_->GetBytesBuffer(buffer, maxLen); | |
54 if (rv == 0) | |
55 streamStatus_ = NSStreamStatusAtEnd; | |
56 else if (rv < 0) | |
57 streamStatus_ = NSStreamStatusError; | |
58 else | |
59 streamStatus_ = NSStreamStatusReading; | |
Mark Mentovai
2014/10/30 21:13:54
The documentation for this status says that it’s s
Robert Sesek
2014/10/31 14:48:15
Done.
| |
60 return rv; | |
61 } | |
62 | |
63 - (BOOL)getBuffer:(uint8_t**)buffer length:(NSUInteger*)length { | |
64 return NO; | |
65 } | |
66 | |
67 // NSStream: | |
68 | |
69 - (void)scheduleInRunLoop:(NSRunLoop*)runLoop | |
Mark Mentovai
2014/10/30 21:13:55
Can you add a comment about why this is unnecessar
Robert Sesek
2014/10/31 14:48:14
I would if I knew why it were necessary ;). I don'
| |
70 forMode:(NSString*)mode { | |
71 } | |
72 | |
73 - (void)removeFromRunLoop:(NSRunLoop*)runLoop | |
74 forMode:(NSString*)mode { | |
75 } | |
76 | |
77 - (void)open { | |
78 streamStatus_ = NSStreamStatusOpen; | |
79 } | |
80 | |
81 - (void)close { | |
82 streamStatus_ = NSStreamStatusClosed; | |
83 } | |
84 | |
85 - (NSStreamStatus)streamStatus { | |
86 return streamStatus_; | |
87 } | |
88 | |
89 - (id<NSStreamDelegate>)delegate { | |
90 return delegate_; | |
91 } | |
92 | |
93 - (void)setDelegate:(id)delegate { | |
94 delegate_ = delegate; | |
95 } | |
96 | |
97 @end | |
98 | |
99 namespace crashpad { | |
100 | |
101 HTTPTransportMac::HTTPTransportMac() : HTTPTransport() { | |
102 } | |
103 | |
104 HTTPTransportMac::~HTTPTransportMac() { | |
105 } | |
106 | |
107 bool HTTPTransportMac::ExecuteSynchronously() { | |
108 DCHECK(body_stream()); | |
109 | |
110 @autoreleasepool { | |
111 NSString* urlNSString = base::SysUTF8ToNSString(url()); | |
Mark Mentovai
2014/10/30 21:13:54
You’re not in the @implementation anymore here: ur
Robert Sesek
2014/10/31 14:48:14
Done.
| |
112 NSURL* url = [NSURL URLWithString:urlNSString]; | |
113 NSMutableURLRequest* request = | |
114 [NSMutableURLRequest requestWithURL:url | |
115 cachePolicy:NSURLRequestUseProtocolCachePolicy | |
116 timeoutInterval:15.0]; | |
Mark Mentovai
2014/10/30 21:13:55
It’d be reasonable for this to be tunable at the H
Robert Sesek
2014/10/31 14:48:14
Done.
| |
117 [request setHTTPMethod:base::SysUTF8ToNSString(method())]; | |
118 | |
119 for (const auto& pair : headers()) { | |
120 [request setValue:base::SysUTF8ToNSString(pair.second) | |
121 forHTTPHeaderField:base::SysUTF8ToNSString(pair.first)]; | |
122 } | |
123 | |
124 base::scoped_nsobject<CrashpadHTTPBodyStreamTransport> transport( | |
125 [[CrashpadHTTPBodyStreamTransport alloc] initWithBodyStream: | |
126 body_stream()]); | |
127 [request setHTTPBodyStream:transport.get()]; | |
128 | |
129 NSURLResponse* response = nil; | |
130 NSError* error = nil; | |
131 [NSURLConnection sendSynchronousRequest:request | |
132 returningResponse:&response | |
133 error:&error]; | |
134 | |
135 if (error) { | |
136 LOG(ERROR) << [[error localizedDescription] UTF8String] << " (" | |
137 << [[error domain] UTF8String] << " " << [error code] << ")"; | |
138 return false; | |
139 } | |
140 if (!response) { | |
141 LOG(ERROR) << "no response"; | |
142 return false; | |
143 } | |
144 NSHTTPURLResponse* httpResponse = | |
145 base::mac::ObjCCast<NSHTTPURLResponse>(response); | |
146 if (!httpResponse) { | |
147 LOG(ERROR) << "no httpResponse"; | |
148 return false; | |
149 } | |
150 NSInteger httpStatus = [httpResponse statusCode]; | |
151 if (httpStatus != 200) { | |
152 LOG(ERROR) << base::StringPrintf("HTTP status %ld", | |
153 static_cast<long>(httpStatus)); | |
154 return false; | |
155 } | |
156 | |
157 return true; | |
158 } | |
159 } | |
160 | |
161 } // namespace crashpad | |
OLD | NEW |