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