apply custom request header for every request type (ios)

This commit is contained in:
Jeff Oh 2015-02-12 20:07:19 +09:00
parent 4de2e6b9bf
commit e33854f3eb
1 changed files with 17 additions and 16 deletions

View File

@ -167,6 +167,23 @@ static int processTask(HttpRequest *request, NSString* requestType, void *stream
//set request type
[nsrequest setHTTPMethod:requestType];
/* get custom header data (if set) */
std::vector<std::string> headers=request->getHeaders();
if(!headers.empty())
{
/* append custom headers one by one */
for (std::vector<std::string>::iterator it = headers.begin(); it != headers.end(); ++it)
{
unsigned long i = it->find(':', 0);
unsigned long length = it->size();
std::string field = it->substr(0, i);
std::string value = it->substr(i+1, length-i);
NSString *headerField = [NSString stringWithUTF8String:field.c_str()];
NSString *headerValue = [NSString stringWithUTF8String:value.c_str()];
[nsrequest setValue:headerValue forHTTPHeaderField:headerField];
}
}
//if request type is post or put,set header and data
if([requestType isEqual: @"POST"] || [requestType isEqual: @"PUT"])
{
@ -174,22 +191,6 @@ static int processTask(HttpRequest *request, NSString* requestType, void *stream
{
[nsrequest setValue: @"application/x-www-form-urlencoded" forHTTPHeaderField: @"Content-Type"];
}
/* get custom header data (if set) */
std::vector<std::string> headers=request->getHeaders();
if(!headers.empty())
{
/* append custom headers one by one */
for (std::vector<std::string>::iterator it = headers.begin(); it != headers.end(); ++it)
{
unsigned long i = it->find(':', 0);
unsigned long length = it->size();
std::string field = it->substr(0, i);
std::string value = it->substr(i+1, length-i);
NSString *headerField = [NSString stringWithUTF8String:field.c_str()];
NSString *headerValue = [NSString stringWithUTF8String:value.c_str()];
[nsrequest setValue:headerValue forHTTPHeaderField:headerField];
}
}
char* requestDataBuffer = request->getRequestData();
if (nullptr != requestDataBuffer && 0 != strlen(requestDataBuffer))