| 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, |
| (...skipping 98 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 109 | 109 |
| 110 namespace crashpad { | 110 namespace crashpad { |
| 111 | 111 |
| 112 namespace { | 112 namespace { |
| 113 | 113 |
| 114 class HTTPTransportMac final : public HTTPTransport { | 114 class HTTPTransportMac final : public HTTPTransport { |
| 115 public: | 115 public: |
| 116 HTTPTransportMac(); | 116 HTTPTransportMac(); |
| 117 ~HTTPTransportMac() override; | 117 ~HTTPTransportMac() override; |
| 118 | 118 |
| 119 bool ExecuteSynchronously() override; | 119 bool ExecuteSynchronously(std::string* response_body) override; |
| 120 | 120 |
| 121 private: | 121 private: |
| 122 DISALLOW_COPY_AND_ASSIGN(HTTPTransportMac); | 122 DISALLOW_COPY_AND_ASSIGN(HTTPTransportMac); |
| 123 }; | 123 }; |
| 124 | 124 |
| 125 HTTPTransportMac::HTTPTransportMac() : HTTPTransport() { | 125 HTTPTransportMac::HTTPTransportMac() : HTTPTransport() { |
| 126 } | 126 } |
| 127 | 127 |
| 128 HTTPTransportMac::~HTTPTransportMac() { | 128 HTTPTransportMac::~HTTPTransportMac() { |
| 129 } | 129 } |
| 130 | 130 |
| 131 bool HTTPTransportMac::ExecuteSynchronously() { | 131 bool HTTPTransportMac::ExecuteSynchronously(std::string* response_body) { |
| 132 DCHECK(body_stream()); | 132 DCHECK(body_stream()); |
| 133 | 133 |
| 134 @autoreleasepool { | 134 @autoreleasepool { |
| 135 NSString* url_ns_string = base::SysUTF8ToNSString(url()); | 135 NSString* url_ns_string = base::SysUTF8ToNSString(url()); |
| 136 NSURL* url = [NSURL URLWithString:url_ns_string]; | 136 NSURL* url = [NSURL URLWithString:url_ns_string]; |
| 137 NSMutableURLRequest* request = | 137 NSMutableURLRequest* request = |
| 138 [NSMutableURLRequest requestWithURL:url | 138 [NSMutableURLRequest requestWithURL:url |
| 139 cachePolicy:NSURLRequestUseProtocolCachePolicy | 139 cachePolicy:NSURLRequestUseProtocolCachePolicy |
| 140 timeoutInterval:timeout()]; | 140 timeoutInterval:timeout()]; |
| 141 [request setHTTPMethod:base::SysUTF8ToNSString(method())]; | 141 [request setHTTPMethod:base::SysUTF8ToNSString(method())]; |
| 142 | 142 |
| 143 for (const auto& pair : headers()) { | 143 for (const auto& pair : headers()) { |
| 144 [request setValue:base::SysUTF8ToNSString(pair.second) | 144 [request setValue:base::SysUTF8ToNSString(pair.second) |
| 145 forHTTPHeaderField:base::SysUTF8ToNSString(pair.first)]; | 145 forHTTPHeaderField:base::SysUTF8ToNSString(pair.first)]; |
| 146 } | 146 } |
| 147 | 147 |
| 148 base::scoped_nsobject<CrashpadHTTPBodyStreamTransport> transport( | 148 base::scoped_nsobject<CrashpadHTTPBodyStreamTransport> transport( |
| 149 [[CrashpadHTTPBodyStreamTransport alloc] initWithBodyStream: | 149 [[CrashpadHTTPBodyStreamTransport alloc] initWithBodyStream: |
| 150 body_stream()]); | 150 body_stream()]); |
| 151 [request setHTTPBodyStream:transport.get()]; | 151 [request setHTTPBodyStream:transport.get()]; |
| 152 | 152 |
| 153 NSURLResponse* response = nil; | 153 NSURLResponse* response = nil; |
| 154 NSError* error = nil; | 154 NSError* error = nil; |
| 155 [NSURLConnection sendSynchronousRequest:request | 155 NSData* body = [NSURLConnection sendSynchronousRequest:request |
| 156 returningResponse:&response | 156 returningResponse:&response |
| 157 error:&error]; | 157 error:&error]; |
| 158 | 158 |
| 159 if (error) { | 159 if (error) { |
| 160 LOG(ERROR) << [[error localizedDescription] UTF8String] << " (" | 160 LOG(ERROR) << [[error localizedDescription] UTF8String] << " (" |
| 161 << [[error domain] UTF8String] << " " << [error code] << ")"; | 161 << [[error domain] UTF8String] << " " << [error code] << ")"; |
| 162 return false; | 162 return false; |
| 163 } | 163 } |
| 164 if (!response) { | 164 if (!response) { |
| 165 LOG(ERROR) << "no response"; | 165 LOG(ERROR) << "no response"; |
| 166 return false; | 166 return false; |
| 167 } | 167 } |
| 168 NSHTTPURLResponse* http_response = | 168 NSHTTPURLResponse* http_response = |
| 169 base::mac::ObjCCast<NSHTTPURLResponse>(response); | 169 base::mac::ObjCCast<NSHTTPURLResponse>(response); |
| 170 if (!http_response) { | 170 if (!http_response) { |
| 171 LOG(ERROR) << "no http_response"; | 171 LOG(ERROR) << "no http_response"; |
| 172 return false; | 172 return false; |
| 173 } | 173 } |
| 174 NSInteger http_status = [http_response statusCode]; | 174 NSInteger http_status = [http_response statusCode]; |
| 175 if (http_status != 200) { | 175 if (http_status != 200) { |
| 176 LOG(ERROR) << base::StringPrintf("HTTP status %ld", | 176 LOG(ERROR) << base::StringPrintf("HTTP status %ld", |
| 177 implicit_cast<long>(http_status)); | 177 implicit_cast<long>(http_status)); |
| 178 return false; | 178 return false; |
| 179 } | 179 } |
| 180 | 180 |
| 181 if (response_body) { |
| 182 response_body->assign(static_cast<const char*>([body bytes]), |
| 183 [body length]); |
| 184 } |
| 185 |
| 181 return true; | 186 return true; |
| 182 } | 187 } |
| 183 } | 188 } |
| 184 | 189 |
| 185 } // namespace | 190 } // namespace |
| 186 | 191 |
| 187 // static | 192 // static |
| 188 scoped_ptr<HTTPTransport> HTTPTransport::Create() { | 193 scoped_ptr<HTTPTransport> HTTPTransport::Create() { |
| 189 return scoped_ptr<HTTPTransport>(new HTTPTransportMac()); | 194 return scoped_ptr<HTTPTransport>(new HTTPTransportMac()); |
| 190 } | 195 } |
| 191 | 196 |
| 192 } // namespace crashpad | 197 } // namespace crashpad |
| OLD | NEW |