Open SamQuest opened 11 years ago
This is awesome (albeit a little intimidating). Any chance you could point to a working example where you proxy a URL request?
@CodeStrumpet you need to implement your own NSObject<HTTPResponse>
but not work with sockets - just use NSURLSession
and data tasks to serve request from connection and use data task's response to create HTTPMessage
response.
Core functionality may look like this:
- (id)initWithLocalRequest:(HTTPMessage *)request forConnection:(HTTPConnection *)parent targetHost:(NSURL*)targetHost
{
if ((self = [super init]))
{
_connection = parent;
_localRequest = request;
_done = NO;
NSURL *remoteURI = [targetHost copy];
... <some URL changes>
NSURLSession *session = [NSURLSession sessionWithConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration]];
NSMutableURLRequest *urlrequest = [NSMutableURLRequest requestWithURL:remoteURI];
[urlrequest setHTTPMethod:[_localRequest method]];
// Body
NSData *bodyData = [_localRequest body];
if (bodyData.length > 0) {
NSLog(@"bodyData: %@", [[NSString alloc] initWithData:bodyData encoding:NSUTF8StringEncoding]);
[urlrequest setHTTPBody:bodyData];
}
// Headers
NSDictionary *originHeaders = [_localRequest allHeaderFields];
[urlrequest setAllHTTPHeaderFields:originHeaders];
[urlrequest setValue:remoteURI.host forHTTPHeaderField:@"Host"];
// Handler
NSURLSessionTask *task;
task = [session dataTaskWithRequest:urlrequest completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
NSURL *url = [response URL];
_remoteResponse = [self responseForResponse:response data:data];
_done = YES;
[_connection responseHasAvailableData:self];
_connection = nil;
}];
// Run
[task resume];
}
return self;
}
Hello Robbiehanson,
To start, I am really really new to IOS / Objective C.
I am given a project which already uses your server. The enhancement needed to decrypt a progressive download video. In my research a couple of people have asked you and implemented this, but I could not find any code.
So I went on and mucked HTTPAsyncFileResponse myself. Now while waiting for my hairs to grow back, the code works fine. At this state (my knowledge about the language and framework is almost nil) I am satisfied with this code.
This code is for anyone who don't want to start clean. and over the top of your head if anyone see any big issues/leaks please comment.
HTTPConnection.m
HTTPProxyResponse.h
HTTPProxyResponse.m