2012-04-14 22:19:31 +04:00
|
|
|
/**
|
2012-10-09 07:02:04 +04:00
|
|
|
* FreeRDP: A Remote Desktop Protocol Implementation
|
2012-04-14 22:19:31 +04:00
|
|
|
* Hypertext Transfer Protocol (HTTP)
|
|
|
|
*
|
|
|
|
* Copyright 2012 Marc-Andre Moreau <marcandre.moreau@gmail.com>
|
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
|
2012-08-15 01:09:01 +04:00
|
|
|
#ifdef HAVE_CONFIG_H
|
|
|
|
#include "config.h"
|
|
|
|
#endif
|
|
|
|
|
2012-09-09 02:47:28 +04:00
|
|
|
#include <winpr/crt.h>
|
2012-11-14 03:57:46 +04:00
|
|
|
#include <winpr/print.h>
|
2012-12-14 05:23:37 +04:00
|
|
|
#include <winpr/stream.h>
|
2013-05-07 00:57:39 +04:00
|
|
|
#include <winpr/string.h>
|
2012-09-09 02:47:28 +04:00
|
|
|
|
2015-02-03 02:50:26 +03:00
|
|
|
#include <freerdp/log.h>
|
|
|
|
|
2014-05-21 19:32:14 +04:00
|
|
|
#ifdef HAVE_VALGRIND_MEMCHECK_H
|
|
|
|
#include <valgrind/memcheck.h>
|
|
|
|
#endif
|
|
|
|
|
2012-04-14 22:19:31 +04:00
|
|
|
#include "http.h"
|
|
|
|
|
2015-02-03 02:50:26 +03:00
|
|
|
#define TAG FREERDP_TAG("core.gateway.http")
|
2014-08-18 19:22:43 +04:00
|
|
|
|
2015-02-03 01:16:32 +03:00
|
|
|
static char* string_strnstr(const char* str1, const char* str2, size_t slen)
|
|
|
|
{
|
|
|
|
char c, sc;
|
|
|
|
size_t len;
|
|
|
|
|
|
|
|
if ((c = *str2++) != '\0')
|
|
|
|
{
|
|
|
|
len = strlen(str2);
|
|
|
|
|
|
|
|
do
|
|
|
|
{
|
|
|
|
do
|
|
|
|
{
|
|
|
|
if (slen-- < 1 || (sc = *str1++) == '\0')
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
while(sc != c);
|
|
|
|
|
|
|
|
if (len > slen)
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
while(strncmp(str1, str2, len) != 0);
|
|
|
|
|
|
|
|
str1--;
|
|
|
|
}
|
|
|
|
|
|
|
|
return ((char*) str1);
|
|
|
|
}
|
|
|
|
|
|
|
|
static BOOL strings_equals_nocase(void* obj1, void* obj2)
|
|
|
|
{
|
|
|
|
if (!obj1 || !obj2)
|
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
return _stricmp(obj1, obj2) == 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void string_free(void* obj1)
|
|
|
|
{
|
|
|
|
free(obj1);
|
|
|
|
}
|
|
|
|
|
2014-08-18 21:34:47 +04:00
|
|
|
HttpContext* http_context_new()
|
2012-04-14 22:19:31 +04:00
|
|
|
{
|
2015-02-02 04:47:43 +03:00
|
|
|
return (HttpContext*) calloc(1, sizeof(HttpContext));
|
2012-04-14 22:19:31 +04:00
|
|
|
}
|
|
|
|
|
2015-02-02 04:47:43 +03:00
|
|
|
BOOL http_context_set_method(HttpContext* context, const char* Method)
|
2012-04-14 22:19:31 +04:00
|
|
|
{
|
2015-02-02 04:47:43 +03:00
|
|
|
free(context->Method);
|
|
|
|
context->Method = _strdup(Method);
|
2012-05-06 08:53:07 +04:00
|
|
|
|
2015-02-02 04:47:43 +03:00
|
|
|
if (!context->Method)
|
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
return TRUE;
|
2012-04-14 22:19:31 +04:00
|
|
|
}
|
|
|
|
|
2015-02-02 04:47:43 +03:00
|
|
|
BOOL http_context_set_uri(HttpContext* context, const char* URI)
|
2012-04-14 22:19:31 +04:00
|
|
|
{
|
2015-02-02 04:47:43 +03:00
|
|
|
free(context->URI);
|
|
|
|
context->URI = _strdup(URI);
|
2012-05-06 08:53:07 +04:00
|
|
|
|
2015-02-02 04:47:43 +03:00
|
|
|
if (!context->URI)
|
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
return TRUE;
|
2012-04-14 22:19:31 +04:00
|
|
|
}
|
|
|
|
|
2015-02-02 04:47:43 +03:00
|
|
|
BOOL http_context_set_user_agent(HttpContext* context, const char* UserAgent)
|
2012-04-14 22:19:31 +04:00
|
|
|
{
|
2015-02-02 04:47:43 +03:00
|
|
|
free(context->UserAgent);
|
|
|
|
context->UserAgent = _strdup(UserAgent);
|
2012-05-06 08:53:07 +04:00
|
|
|
|
2015-02-02 04:47:43 +03:00
|
|
|
if (!context->UserAgent)
|
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
return TRUE;
|
2012-04-14 22:19:31 +04:00
|
|
|
}
|
|
|
|
|
2015-02-02 04:47:43 +03:00
|
|
|
BOOL http_context_set_host(HttpContext* context, const char* Host)
|
2012-04-14 22:19:31 +04:00
|
|
|
{
|
2015-02-02 04:47:43 +03:00
|
|
|
free(context->Host);
|
|
|
|
context->Host = _strdup(Host);
|
|
|
|
|
|
|
|
if (!context->Host)
|
|
|
|
return FALSE;
|
2012-05-06 08:53:07 +04:00
|
|
|
|
2015-02-02 04:47:43 +03:00
|
|
|
return TRUE;
|
2012-04-14 22:19:31 +04:00
|
|
|
}
|
|
|
|
|
2015-02-02 04:47:43 +03:00
|
|
|
BOOL http_context_set_accept(HttpContext* context, const char* Accept)
|
2012-04-14 22:19:31 +04:00
|
|
|
{
|
2015-02-02 04:47:43 +03:00
|
|
|
free(context->Accept);
|
|
|
|
context->Accept = _strdup(Accept);
|
|
|
|
|
|
|
|
if (!context->Accept)
|
|
|
|
return FALSE;
|
2012-05-06 08:53:07 +04:00
|
|
|
|
2015-02-02 04:47:43 +03:00
|
|
|
return TRUE;
|
2012-04-14 22:19:31 +04:00
|
|
|
}
|
|
|
|
|
2015-02-02 04:47:43 +03:00
|
|
|
BOOL http_context_set_cache_control(HttpContext* context, const char* CacheControl)
|
2012-04-14 22:19:31 +04:00
|
|
|
{
|
2015-02-02 04:47:43 +03:00
|
|
|
free(context->CacheControl);
|
|
|
|
context->CacheControl = _strdup(CacheControl);
|
|
|
|
|
|
|
|
if (!context->CacheControl)
|
|
|
|
return FALSE;
|
2012-05-06 08:53:07 +04:00
|
|
|
|
2015-02-02 04:47:43 +03:00
|
|
|
return TRUE;
|
2012-04-14 22:19:31 +04:00
|
|
|
}
|
|
|
|
|
2015-02-02 04:47:43 +03:00
|
|
|
BOOL http_context_set_connection(HttpContext* context, const char* Connection)
|
2012-04-14 22:19:31 +04:00
|
|
|
{
|
2015-02-02 04:47:43 +03:00
|
|
|
free(context->Connection);
|
|
|
|
context->Connection = _strdup(Connection);
|
|
|
|
|
|
|
|
if (!context->Connection)
|
|
|
|
return FALSE;
|
2012-05-06 08:53:07 +04:00
|
|
|
|
2015-02-02 04:47:43 +03:00
|
|
|
return TRUE;
|
2012-04-14 22:19:31 +04:00
|
|
|
}
|
|
|
|
|
2015-02-02 04:47:43 +03:00
|
|
|
BOOL http_context_set_pragma(HttpContext* context, const char* Pragma)
|
2012-04-14 22:19:31 +04:00
|
|
|
{
|
2015-02-02 04:47:43 +03:00
|
|
|
free(context->Pragma);
|
|
|
|
context->Pragma = _strdup(Pragma);
|
|
|
|
|
|
|
|
if (!context->Pragma)
|
|
|
|
return FALSE;
|
2012-05-06 08:53:07 +04:00
|
|
|
|
2015-02-02 04:47:43 +03:00
|
|
|
return TRUE;
|
2012-04-14 22:19:31 +04:00
|
|
|
}
|
|
|
|
|
2015-03-17 21:54:16 +03:00
|
|
|
BOOL http_context_set_rdg_connection_id(HttpContext* context, const char* RdgConnectionId)
|
|
|
|
{
|
|
|
|
free(context->RdgConnectionId);
|
|
|
|
context->RdgConnectionId = _strdup(RdgConnectionId);
|
|
|
|
|
|
|
|
if (!context->RdgConnectionId)
|
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
2015-02-02 04:47:43 +03:00
|
|
|
void http_context_free(HttpContext* context)
|
2012-04-14 22:19:31 +04:00
|
|
|
{
|
2015-02-02 04:47:43 +03:00
|
|
|
if (context)
|
2012-04-14 22:19:31 +04:00
|
|
|
{
|
2015-02-02 04:47:43 +03:00
|
|
|
free(context->UserAgent);
|
|
|
|
free(context->Host);
|
|
|
|
free(context->URI);
|
|
|
|
free(context->Accept);
|
|
|
|
free(context->Method);
|
|
|
|
free(context->CacheControl);
|
|
|
|
free(context->Connection);
|
|
|
|
free(context->Pragma);
|
2015-03-17 21:54:16 +03:00
|
|
|
free(context->RdgConnectionId);
|
2015-02-02 04:47:43 +03:00
|
|
|
free(context);
|
2012-04-14 22:19:31 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-02-02 04:47:43 +03:00
|
|
|
BOOL http_request_set_method(HttpRequest* request, const char* Method)
|
2012-04-14 22:19:31 +04:00
|
|
|
{
|
2015-02-02 04:47:43 +03:00
|
|
|
free(request->Method);
|
|
|
|
request->Method = _strdup(Method);
|
|
|
|
|
|
|
|
if (!request->Method)
|
|
|
|
return FALSE;
|
2012-05-06 08:53:07 +04:00
|
|
|
|
2015-02-02 04:47:43 +03:00
|
|
|
return TRUE;
|
2012-04-14 22:19:31 +04:00
|
|
|
}
|
|
|
|
|
2015-02-02 04:47:43 +03:00
|
|
|
BOOL http_request_set_uri(HttpRequest* request, const char* URI)
|
2012-04-14 22:19:31 +04:00
|
|
|
{
|
2015-02-02 04:47:43 +03:00
|
|
|
free(request->URI);
|
|
|
|
request->URI = _strdup(URI);
|
2012-05-06 08:53:07 +04:00
|
|
|
|
2015-02-02 04:47:43 +03:00
|
|
|
if (!request->URI)
|
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
return TRUE;
|
2012-04-14 22:19:31 +04:00
|
|
|
}
|
|
|
|
|
2015-02-02 04:47:43 +03:00
|
|
|
BOOL http_request_set_auth_scheme(HttpRequest* request, const char* AuthScheme)
|
2012-04-17 00:21:46 +04:00
|
|
|
{
|
2015-02-02 04:47:43 +03:00
|
|
|
free(request->AuthScheme);
|
|
|
|
request->AuthScheme = _strdup(AuthScheme);
|
2012-05-06 08:53:07 +04:00
|
|
|
|
2015-02-02 04:47:43 +03:00
|
|
|
if (!request->AuthScheme)
|
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
return TRUE;
|
2012-04-17 00:21:46 +04:00
|
|
|
}
|
|
|
|
|
2015-02-02 04:47:43 +03:00
|
|
|
BOOL http_request_set_auth_param(HttpRequest* request, const char* AuthParam)
|
2012-04-17 00:21:46 +04:00
|
|
|
{
|
2015-02-02 04:47:43 +03:00
|
|
|
free(request->AuthParam);
|
|
|
|
request->AuthParam = _strdup(AuthParam);
|
2012-05-06 08:53:07 +04:00
|
|
|
|
2015-02-02 04:47:43 +03:00
|
|
|
if (!request->AuthParam)
|
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
return TRUE;
|
2012-04-17 00:21:46 +04:00
|
|
|
}
|
|
|
|
|
2015-03-17 21:54:16 +03:00
|
|
|
BOOL http_request_set_transfer_encoding(HttpRequest* request, const char* TransferEncoding)
|
|
|
|
{
|
|
|
|
free(request->TransferEncoding);
|
|
|
|
request->TransferEncoding = _strdup(TransferEncoding);
|
|
|
|
|
|
|
|
if (!request->TransferEncoding)
|
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
2014-08-18 21:34:47 +04:00
|
|
|
char* http_encode_body_line(char* param, char* value)
|
2012-09-11 23:10:37 +04:00
|
|
|
{
|
2014-08-18 21:34:47 +04:00
|
|
|
char* line;
|
2012-09-11 23:10:37 +04:00
|
|
|
int length;
|
2015-02-02 04:47:43 +03:00
|
|
|
|
2012-09-11 23:10:37 +04:00
|
|
|
length = strlen(param) + strlen(value) + 2;
|
2014-08-18 21:34:47 +04:00
|
|
|
line = (char*) malloc(length + 1);
|
2014-08-18 19:22:43 +04:00
|
|
|
|
2014-04-09 17:00:38 +04:00
|
|
|
if (!line)
|
|
|
|
return NULL;
|
2012-09-11 23:10:37 +04:00
|
|
|
|
2014-08-18 19:22:43 +04:00
|
|
|
sprintf_s(line, length + 1, "%s: %s", param, value);
|
2012-09-11 23:10:37 +04:00
|
|
|
return line;
|
|
|
|
}
|
|
|
|
|
2014-08-18 21:34:47 +04:00
|
|
|
char* http_encode_content_length_line(int ContentLength)
|
2012-09-11 23:10:37 +04:00
|
|
|
{
|
2014-08-18 21:34:47 +04:00
|
|
|
char* line;
|
2012-09-11 23:10:37 +04:00
|
|
|
int length;
|
|
|
|
char str[32];
|
2015-02-02 04:47:43 +03:00
|
|
|
|
2012-09-11 23:10:37 +04:00
|
|
|
_itoa_s(ContentLength, str, sizeof(str), 10);
|
|
|
|
length = strlen("Content-Length") + strlen(str) + 2;
|
2015-02-02 04:47:43 +03:00
|
|
|
line = (char*) malloc(length + 1);
|
2014-08-18 19:22:43 +04:00
|
|
|
|
2014-04-09 17:00:38 +04:00
|
|
|
if (!line)
|
|
|
|
return NULL;
|
2012-09-11 23:10:37 +04:00
|
|
|
|
2014-08-18 19:22:43 +04:00
|
|
|
sprintf_s(line, length + 1, "Content-Length: %s", str);
|
2012-09-11 23:10:37 +04:00
|
|
|
return line;
|
|
|
|
}
|
|
|
|
|
2014-08-18 21:34:47 +04:00
|
|
|
char* http_encode_header_line(char* Method, char* URI)
|
2012-09-11 23:10:37 +04:00
|
|
|
{
|
2014-08-18 21:34:47 +04:00
|
|
|
char* line;
|
2012-09-11 23:10:37 +04:00
|
|
|
int length;
|
2015-02-02 04:47:43 +03:00
|
|
|
|
2012-09-11 23:10:37 +04:00
|
|
|
length = strlen("HTTP/1.1") + strlen(Method) + strlen(URI) + 2;
|
2014-08-18 21:34:47 +04:00
|
|
|
line = (char*)malloc(length + 1);
|
2014-08-18 19:22:43 +04:00
|
|
|
|
2014-04-09 17:00:38 +04:00
|
|
|
if (!line)
|
|
|
|
return NULL;
|
2012-09-11 23:10:37 +04:00
|
|
|
|
2014-08-18 19:22:43 +04:00
|
|
|
sprintf_s(line, length + 1, "%s %s HTTP/1.1", Method, URI);
|
2012-09-11 23:10:37 +04:00
|
|
|
return line;
|
|
|
|
}
|
|
|
|
|
2014-08-18 21:34:47 +04:00
|
|
|
char* http_encode_authorization_line(char* AuthScheme, char* AuthParam)
|
2012-09-11 23:10:37 +04:00
|
|
|
{
|
2014-08-18 21:34:47 +04:00
|
|
|
char* line;
|
2012-09-11 23:10:37 +04:00
|
|
|
int length;
|
2015-02-02 04:47:43 +03:00
|
|
|
|
2012-09-11 23:10:37 +04:00
|
|
|
length = strlen("Authorization") + strlen(AuthScheme) + strlen(AuthParam) + 3;
|
2014-08-18 21:34:47 +04:00
|
|
|
line = (char*) malloc(length + 1);
|
2014-08-18 19:22:43 +04:00
|
|
|
|
2014-04-09 17:00:38 +04:00
|
|
|
if (!line)
|
|
|
|
return NULL;
|
2012-09-11 23:10:37 +04:00
|
|
|
|
2014-08-18 19:22:43 +04:00
|
|
|
sprintf_s(line, length + 1, "Authorization: %s %s", AuthScheme, AuthParam);
|
2012-09-11 23:10:37 +04:00
|
|
|
return line;
|
|
|
|
}
|
|
|
|
|
2015-02-02 04:47:43 +03:00
|
|
|
wStream* http_request_write(HttpContext* context, HttpRequest* request)
|
2012-04-14 22:19:31 +04:00
|
|
|
{
|
2015-02-02 04:47:43 +03:00
|
|
|
wStream* s;
|
2014-04-09 17:00:38 +04:00
|
|
|
int i, count;
|
2014-08-18 21:34:47 +04:00
|
|
|
char** lines;
|
2012-04-14 22:19:31 +04:00
|
|
|
int length = 0;
|
2015-01-14 00:35:34 +03:00
|
|
|
|
|
|
|
count = 0;
|
|
|
|
lines = (char**) calloc(32, sizeof(char*));
|
2014-08-18 19:22:43 +04:00
|
|
|
|
2014-04-09 17:00:38 +04:00
|
|
|
if (!lines)
|
|
|
|
return NULL;
|
|
|
|
|
2015-02-02 04:47:43 +03:00
|
|
|
lines[count++] = http_encode_header_line(request->Method, request->URI);
|
|
|
|
lines[count++] = http_encode_body_line("Cache-Control", context->CacheControl);
|
|
|
|
lines[count++] = http_encode_body_line("Connection", context->Connection);
|
|
|
|
lines[count++] = http_encode_body_line("Pragma", context->Pragma);
|
|
|
|
lines[count++] = http_encode_body_line("Accept", context->Accept);
|
|
|
|
lines[count++] = http_encode_body_line("User-Agent", context->UserAgent);
|
|
|
|
lines[count++] = http_encode_content_length_line(request->ContentLength);
|
|
|
|
lines[count++] = http_encode_body_line("Host", context->Host);
|
2014-04-09 17:00:38 +04:00
|
|
|
|
|
|
|
/* check that everything went well */
|
2015-01-14 00:35:34 +03:00
|
|
|
for (i = 0; i < count; i++)
|
2014-04-09 17:00:38 +04:00
|
|
|
{
|
|
|
|
if (!lines[i])
|
|
|
|
goto out_free;
|
|
|
|
}
|
2012-04-17 00:21:46 +04:00
|
|
|
|
2015-03-17 21:54:16 +03:00
|
|
|
if (context->RdgConnectionId)
|
|
|
|
{
|
|
|
|
lines[count] = http_encode_body_line("RDG-Connection-Id", context->RdgConnectionId);
|
|
|
|
|
|
|
|
if (!lines[count])
|
|
|
|
goto out_free;
|
|
|
|
|
|
|
|
count++;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (request->TransferEncoding)
|
|
|
|
{
|
|
|
|
lines[count] = http_encode_body_line("Transfer-Encoding", request->TransferEncoding);
|
|
|
|
|
|
|
|
if (!lines[count])
|
|
|
|
goto out_free;
|
|
|
|
|
|
|
|
count++;
|
|
|
|
}
|
|
|
|
|
2015-02-02 04:47:43 +03:00
|
|
|
if (request->Authorization)
|
2012-04-17 00:21:46 +04:00
|
|
|
{
|
2015-02-02 04:47:43 +03:00
|
|
|
lines[count] = http_encode_body_line("Authorization", request->Authorization);
|
2014-08-18 19:22:43 +04:00
|
|
|
|
2015-01-14 00:35:34 +03:00
|
|
|
if (!lines[count])
|
2014-04-09 17:00:38 +04:00
|
|
|
goto out_free;
|
2015-01-14 00:35:34 +03:00
|
|
|
|
|
|
|
count++;
|
2012-04-17 00:21:46 +04:00
|
|
|
}
|
2015-02-02 04:47:43 +03:00
|
|
|
else if (request->AuthScheme && request->AuthParam)
|
2012-04-17 00:21:46 +04:00
|
|
|
{
|
2015-02-02 04:47:43 +03:00
|
|
|
lines[count] = http_encode_authorization_line(request->AuthScheme, request->AuthParam);
|
2014-08-18 19:22:43 +04:00
|
|
|
|
2015-01-14 00:35:34 +03:00
|
|
|
if (!lines[count])
|
2014-04-09 17:00:38 +04:00
|
|
|
goto out_free;
|
2015-01-14 00:35:34 +03:00
|
|
|
|
|
|
|
count++;
|
2012-04-17 00:21:46 +04:00
|
|
|
}
|
2012-04-14 22:19:31 +04:00
|
|
|
|
2014-04-09 17:00:38 +04:00
|
|
|
for (i = 0; i < count; i++)
|
2012-04-14 22:19:31 +04:00
|
|
|
{
|
2014-04-09 17:00:38 +04:00
|
|
|
length += (strlen(lines[i]) + 2); /* add +2 for each '\r\n' character */
|
2012-04-14 22:19:31 +04:00
|
|
|
}
|
2014-08-18 19:22:43 +04:00
|
|
|
|
2012-10-29 02:01:18 +04:00
|
|
|
length += 2; /* empty line "\r\n" at end of header */
|
2012-04-17 00:21:46 +04:00
|
|
|
length += 1; /* null terminator */
|
2012-12-14 05:23:37 +04:00
|
|
|
s = Stream_New(NULL, length);
|
2014-08-18 19:22:43 +04:00
|
|
|
|
2014-04-09 17:00:38 +04:00
|
|
|
if (!s)
|
|
|
|
goto out_free;
|
2012-04-14 22:19:31 +04:00
|
|
|
|
2014-04-09 17:00:38 +04:00
|
|
|
for (i = 0; i < count; i++)
|
2012-04-14 22:19:31 +04:00
|
|
|
{
|
2014-04-09 17:00:38 +04:00
|
|
|
Stream_Write(s, lines[i], strlen(lines[i]));
|
2012-12-14 05:23:37 +04:00
|
|
|
Stream_Write(s, "\r\n", 2);
|
2014-04-09 17:00:38 +04:00
|
|
|
free(lines[i]);
|
2012-04-14 22:19:31 +04:00
|
|
|
}
|
|
|
|
|
2014-08-18 19:22:43 +04:00
|
|
|
Stream_Write(s, "\r\n", 2);
|
2014-04-09 17:00:38 +04:00
|
|
|
free(lines);
|
2012-12-14 05:23:37 +04:00
|
|
|
Stream_Write(s, "\0", 1); /* append null terminator */
|
|
|
|
Stream_Rewind(s, 1); /* don't include null terminator in length */
|
2016-02-25 22:01:12 +03:00
|
|
|
Stream_SetLength(s, Stream_GetPosition(s));
|
2012-04-14 22:19:31 +04:00
|
|
|
return s;
|
2014-08-18 19:22:43 +04:00
|
|
|
|
2015-05-11 10:07:39 +03:00
|
|
|
out_free:
|
|
|
|
for (i = 0; i < count; i++)
|
|
|
|
free(lines[i]);
|
2014-08-18 19:22:43 +04:00
|
|
|
|
2014-04-09 17:00:38 +04:00
|
|
|
free(lines);
|
|
|
|
return NULL;
|
|
|
|
}
|
2012-04-14 22:19:31 +04:00
|
|
|
|
2014-08-18 21:34:47 +04:00
|
|
|
HttpRequest* http_request_new()
|
2014-04-09 17:00:38 +04:00
|
|
|
{
|
2014-08-18 21:34:47 +04:00
|
|
|
return (HttpRequest*) calloc(1, sizeof(HttpRequest));
|
2012-04-14 22:19:31 +04:00
|
|
|
}
|
|
|
|
|
2015-02-02 04:47:43 +03:00
|
|
|
void http_request_free(HttpRequest* request)
|
2012-04-14 22:19:31 +04:00
|
|
|
{
|
2015-02-02 04:47:43 +03:00
|
|
|
if (!request)
|
2014-04-09 17:00:38 +04:00
|
|
|
return;
|
|
|
|
|
2015-05-11 10:07:39 +03:00
|
|
|
free(request->AuthParam);
|
|
|
|
free(request->AuthScheme);
|
|
|
|
free(request->Authorization);
|
2015-02-02 04:47:43 +03:00
|
|
|
free(request->Content);
|
|
|
|
free(request->Method);
|
|
|
|
free(request->URI);
|
2015-03-17 23:09:17 +03:00
|
|
|
free(request->TransferEncoding);
|
2015-02-02 04:47:43 +03:00
|
|
|
free(request);
|
2012-04-14 22:19:31 +04:00
|
|
|
}
|
2012-04-17 00:21:46 +04:00
|
|
|
|
2015-02-02 04:47:43 +03:00
|
|
|
BOOL http_response_parse_header_status_line(HttpResponse* response, char* status_line)
|
2012-04-17 00:21:46 +04:00
|
|
|
{
|
2014-11-17 02:00:09 +03:00
|
|
|
char* separator = NULL;
|
2014-08-18 21:34:47 +04:00
|
|
|
char* status_code;
|
|
|
|
char* reason_phrase;
|
2014-11-17 02:00:09 +03:00
|
|
|
|
|
|
|
if (status_line)
|
|
|
|
separator = strchr(status_line, ' ');
|
2014-08-18 19:22:43 +04:00
|
|
|
|
2013-05-07 00:57:39 +04:00
|
|
|
if (!separator)
|
|
|
|
return FALSE;
|
2012-04-17 00:21:46 +04:00
|
|
|
|
2014-08-18 19:22:43 +04:00
|
|
|
status_code = separator + 1;
|
2012-04-17 00:21:46 +04:00
|
|
|
separator = strchr(status_code, ' ');
|
2014-08-18 19:22:43 +04:00
|
|
|
|
2013-05-07 00:57:39 +04:00
|
|
|
if (!separator)
|
|
|
|
return FALSE;
|
2012-04-17 00:21:46 +04:00
|
|
|
|
2014-08-18 19:22:43 +04:00
|
|
|
reason_phrase = separator + 1;
|
2012-04-17 00:21:46 +04:00
|
|
|
*separator = '\0';
|
2015-02-02 04:47:43 +03:00
|
|
|
response->StatusCode = atoi(status_code);
|
|
|
|
response->ReasonPhrase = _strdup(reason_phrase);
|
2014-08-18 19:22:43 +04:00
|
|
|
|
2015-02-02 04:47:43 +03:00
|
|
|
if (!response->ReasonPhrase)
|
2014-04-09 17:00:38 +04:00
|
|
|
return FALSE;
|
2014-08-18 19:22:43 +04:00
|
|
|
|
2012-04-17 00:21:46 +04:00
|
|
|
*separator = ' ';
|
2013-05-07 00:57:39 +04:00
|
|
|
return TRUE;
|
2012-04-17 00:21:46 +04:00
|
|
|
}
|
|
|
|
|
2015-02-02 04:47:43 +03:00
|
|
|
BOOL http_response_parse_header_field(HttpResponse* response, char* name, char* value)
|
2012-04-17 00:21:46 +04:00
|
|
|
{
|
2014-12-11 19:25:34 +03:00
|
|
|
BOOL status = TRUE;
|
|
|
|
|
2013-05-07 00:57:39 +04:00
|
|
|
if (_stricmp(name, "Content-Length") == 0)
|
2012-04-17 00:21:46 +04:00
|
|
|
{
|
2015-02-02 04:47:43 +03:00
|
|
|
response->ContentLength = atoi(value);
|
2012-04-17 00:21:46 +04:00
|
|
|
}
|
2015-02-03 01:16:32 +03:00
|
|
|
else if (_stricmp(name, "Content-Type") == 0)
|
|
|
|
{
|
|
|
|
response->ContentType = _strdup(value);
|
|
|
|
|
|
|
|
if (!response->ContentType)
|
|
|
|
return FALSE;
|
|
|
|
}
|
2014-04-19 01:08:34 +04:00
|
|
|
else if (_stricmp(name, "WWW-Authenticate") == 0)
|
2012-04-17 00:21:46 +04:00
|
|
|
{
|
2014-12-11 19:25:34 +03:00
|
|
|
char* separator = NULL;
|
|
|
|
char* authScheme = NULL;
|
|
|
|
char* authValue = NULL;
|
|
|
|
|
2012-04-17 00:21:46 +04:00
|
|
|
separator = strchr(value, ' ');
|
|
|
|
|
2014-12-11 19:25:34 +03:00
|
|
|
if (separator)
|
2012-04-17 00:21:46 +04:00
|
|
|
{
|
2014-04-19 01:08:34 +04:00
|
|
|
/* WWW-Authenticate: Basic realm=""
|
|
|
|
* WWW-Authenticate: NTLM base64token
|
|
|
|
* WWW-Authenticate: Digest realm="testrealm@host.com", qop="auth, auth-int",
|
|
|
|
* nonce="dcd98b7102dd2f0e8b11d0f600bfb0c093",
|
|
|
|
* opaque="5ccc069c403ebaf9f0171e9517f40e41"
|
|
|
|
*/
|
2012-04-17 00:21:46 +04:00
|
|
|
*separator = '\0';
|
2014-04-19 01:08:34 +04:00
|
|
|
authScheme = _strdup(value);
|
|
|
|
authValue = _strdup(separator + 1);
|
2014-08-18 19:22:43 +04:00
|
|
|
|
2014-04-19 01:08:34 +04:00
|
|
|
if (!authScheme || !authValue)
|
2014-04-09 17:00:38 +04:00
|
|
|
return FALSE;
|
2014-08-18 19:22:43 +04:00
|
|
|
|
2012-04-17 00:21:46 +04:00
|
|
|
*separator = ' ';
|
|
|
|
}
|
2014-04-19 01:08:34 +04:00
|
|
|
else
|
2012-04-17 00:21:46 +04:00
|
|
|
{
|
2014-04-19 01:08:34 +04:00
|
|
|
authScheme = _strdup(value);
|
2014-08-18 19:22:43 +04:00
|
|
|
|
2014-04-19 01:08:34 +04:00
|
|
|
if (!authScheme)
|
|
|
|
return FALSE;
|
2014-08-18 19:22:43 +04:00
|
|
|
|
2014-04-19 01:08:34 +04:00
|
|
|
authValue = NULL;
|
2012-04-17 00:21:46 +04:00
|
|
|
}
|
|
|
|
|
2015-02-02 04:47:43 +03:00
|
|
|
status = ListDictionary_Add(response->Authenticates, authScheme, authValue);
|
2012-04-17 00:21:46 +04:00
|
|
|
}
|
2014-08-18 19:22:43 +04:00
|
|
|
|
2014-12-11 19:25:34 +03:00
|
|
|
return status;
|
2012-04-17 00:21:46 +04:00
|
|
|
}
|
|
|
|
|
2015-02-02 04:47:43 +03:00
|
|
|
BOOL http_response_parse_header(HttpResponse* response)
|
2012-04-17 00:21:46 +04:00
|
|
|
{
|
2015-02-03 01:16:32 +03:00
|
|
|
char c;
|
2012-04-17 00:21:46 +04:00
|
|
|
int count;
|
2014-08-18 21:34:47 +04:00
|
|
|
char* line;
|
|
|
|
char* name;
|
|
|
|
char* value;
|
|
|
|
char* colon_pos;
|
|
|
|
char* end_of_header;
|
2013-05-07 00:57:39 +04:00
|
|
|
char end_of_header_char;
|
2012-04-17 00:21:46 +04:00
|
|
|
|
2015-02-02 04:47:43 +03:00
|
|
|
if (!response)
|
2013-08-29 17:30:22 +04:00
|
|
|
return FALSE;
|
|
|
|
|
2015-02-02 04:47:43 +03:00
|
|
|
if (!response->lines)
|
2013-08-29 17:30:22 +04:00
|
|
|
return FALSE;
|
|
|
|
|
2015-02-02 04:47:43 +03:00
|
|
|
if (!http_response_parse_header_status_line(response, response->lines[0]))
|
2013-05-07 00:57:39 +04:00
|
|
|
return FALSE;
|
2012-04-17 00:21:46 +04:00
|
|
|
|
2015-02-02 04:47:43 +03:00
|
|
|
for (count = 1; count < response->count; count++)
|
2012-04-17 00:21:46 +04:00
|
|
|
{
|
2015-02-02 04:47:43 +03:00
|
|
|
line = response->lines[count];
|
2013-05-07 00:57:39 +04:00
|
|
|
/**
|
|
|
|
* name end_of_header
|
|
|
|
* | |
|
|
|
|
* v v
|
|
|
|
* <header name> : <header value>
|
|
|
|
* ^ ^
|
|
|
|
* | |
|
|
|
|
* colon_pos value
|
|
|
|
*/
|
2014-11-17 02:00:09 +03:00
|
|
|
if (line)
|
|
|
|
colon_pos = strchr(line, ':');
|
|
|
|
else
|
|
|
|
colon_pos = NULL;
|
2014-08-18 19:22:43 +04:00
|
|
|
|
2013-05-07 00:57:39 +04:00
|
|
|
if ((colon_pos == NULL) || (colon_pos == line))
|
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
/* retrieve the position just after header name */
|
2014-08-18 19:22:43 +04:00
|
|
|
for (end_of_header = colon_pos; end_of_header != line; end_of_header--)
|
2013-05-07 00:57:39 +04:00
|
|
|
{
|
|
|
|
c = end_of_header[-1];
|
2014-08-18 19:22:43 +04:00
|
|
|
|
2013-05-07 00:57:39 +04:00
|
|
|
if (c != ' ' && c != '\t' && c != ':')
|
|
|
|
break;
|
|
|
|
}
|
2014-08-18 19:22:43 +04:00
|
|
|
|
2013-05-07 00:57:39 +04:00
|
|
|
if (end_of_header == line)
|
|
|
|
return FALSE;
|
2014-08-18 19:22:43 +04:00
|
|
|
|
2013-05-07 00:57:39 +04:00
|
|
|
end_of_header_char = *end_of_header;
|
|
|
|
*end_of_header = '\0';
|
2012-04-17 00:21:46 +04:00
|
|
|
name = line;
|
2013-05-07 00:57:39 +04:00
|
|
|
|
|
|
|
/* eat space and tabs before header value */
|
|
|
|
for (value = colon_pos + 1; *value; value++)
|
|
|
|
{
|
|
|
|
if ((*value != ' ') && (*value != '\t'))
|
|
|
|
break;
|
|
|
|
}
|
2012-04-17 00:21:46 +04:00
|
|
|
|
2015-02-02 04:47:43 +03:00
|
|
|
if (!http_response_parse_header_field(response, name, value))
|
2014-04-09 17:00:38 +04:00
|
|
|
return FALSE;
|
2012-04-17 00:21:46 +04:00
|
|
|
|
2013-05-07 00:57:39 +04:00
|
|
|
*end_of_header = end_of_header_char;
|
2012-04-17 00:21:46 +04:00
|
|
|
}
|
2014-08-18 19:22:43 +04:00
|
|
|
|
2013-05-07 00:57:39 +04:00
|
|
|
return TRUE;
|
2012-04-17 00:21:46 +04:00
|
|
|
}
|
|
|
|
|
2015-02-02 04:47:43 +03:00
|
|
|
void http_response_print(HttpResponse* response)
|
2012-04-22 00:18:07 +04:00
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
2015-02-02 04:47:43 +03:00
|
|
|
for (i = 0; i < response->count; i++)
|
2012-04-22 00:18:07 +04:00
|
|
|
{
|
2015-02-02 04:47:43 +03:00
|
|
|
WLog_ERR(TAG, "%s", response->lines[i]);
|
2012-04-22 00:18:07 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-08-18 21:34:47 +04:00
|
|
|
HttpResponse* http_response_recv(rdpTls* tls)
|
2012-04-17 00:21:46 +04:00
|
|
|
{
|
2015-02-03 01:16:32 +03:00
|
|
|
wStream* s;
|
|
|
|
int size;
|
|
|
|
int count;
|
2012-04-17 00:21:46 +04:00
|
|
|
int status;
|
2015-02-03 01:16:32 +03:00
|
|
|
int position;
|
|
|
|
char* line;
|
|
|
|
char* buffer;
|
2015-06-23 22:29:21 +03:00
|
|
|
char* header = NULL;
|
2015-02-03 01:16:32 +03:00
|
|
|
char* payload;
|
|
|
|
int bodyLength;
|
|
|
|
int payloadOffset;
|
2015-02-02 04:47:43 +03:00
|
|
|
HttpResponse* response;
|
2015-02-02 02:50:21 +03:00
|
|
|
|
2015-03-19 18:44:47 +03:00
|
|
|
size = 2048;
|
2015-02-03 01:16:32 +03:00
|
|
|
payload = NULL;
|
|
|
|
payloadOffset = 0;
|
2014-12-11 19:25:34 +03:00
|
|
|
|
2015-02-03 01:16:32 +03:00
|
|
|
s = Stream_New(NULL, size);
|
2014-08-18 19:22:43 +04:00
|
|
|
|
2015-02-03 01:16:32 +03:00
|
|
|
if (!s)
|
|
|
|
goto out_free;
|
|
|
|
|
|
|
|
buffer = (char*) Stream_Buffer(s);
|
2014-04-09 17:00:38 +04:00
|
|
|
|
2015-02-02 04:47:43 +03:00
|
|
|
response = http_response_new();
|
2014-08-18 19:22:43 +04:00
|
|
|
|
2015-02-02 04:47:43 +03:00
|
|
|
if (!response)
|
2014-04-09 17:00:38 +04:00
|
|
|
goto out_free;
|
2012-04-17 00:21:46 +04:00
|
|
|
|
2015-02-02 04:47:43 +03:00
|
|
|
response->ContentLength = 0;
|
2012-04-17 00:21:46 +04:00
|
|
|
|
2012-10-09 10:31:28 +04:00
|
|
|
while (TRUE)
|
2012-04-17 00:21:46 +04:00
|
|
|
{
|
2015-02-03 01:16:32 +03:00
|
|
|
while (!payloadOffset)
|
2012-11-17 12:45:15 +04:00
|
|
|
{
|
2015-02-03 01:16:32 +03:00
|
|
|
status = BIO_read(tls->bio, Stream_Pointer(s), Stream_Capacity(s) - Stream_GetPosition(s));
|
2014-08-18 19:22:43 +04:00
|
|
|
|
2014-05-21 19:32:14 +04:00
|
|
|
if (status <= 0)
|
|
|
|
{
|
|
|
|
if (!BIO_should_retry(tls->bio))
|
|
|
|
goto out_error;
|
2014-04-09 17:00:38 +04:00
|
|
|
|
2014-05-21 19:32:14 +04:00
|
|
|
USleep(100);
|
2012-11-17 12:45:15 +04:00
|
|
|
continue;
|
2014-05-21 19:32:14 +04:00
|
|
|
}
|
2014-04-09 17:00:38 +04:00
|
|
|
|
2014-05-21 19:32:14 +04:00
|
|
|
#ifdef HAVE_VALGRIND_MEMCHECK_H
|
2015-02-11 17:23:14 +03:00
|
|
|
VALGRIND_MAKE_MEM_DEFINED(Stream_Pointer(s), status);
|
2014-05-21 19:32:14 +04:00
|
|
|
#endif
|
2012-04-17 00:21:46 +04:00
|
|
|
|
2015-02-03 01:16:32 +03:00
|
|
|
Stream_Seek(s, status);
|
2014-08-18 19:22:43 +04:00
|
|
|
|
2015-02-03 01:16:32 +03:00
|
|
|
if (Stream_GetRemainingLength(s) < 1024)
|
|
|
|
{
|
2015-03-30 18:15:45 +03:00
|
|
|
if (!Stream_EnsureRemainingCapacity(s, 1024))
|
2015-03-26 19:09:47 +03:00
|
|
|
goto out_error;
|
2015-02-03 01:16:32 +03:00
|
|
|
buffer = (char*) Stream_Buffer(s);
|
|
|
|
payload = &buffer[payloadOffset];
|
|
|
|
}
|
2012-04-17 00:21:46 +04:00
|
|
|
|
2015-02-03 01:16:32 +03:00
|
|
|
position = Stream_GetPosition(s);
|
2013-05-07 00:57:39 +04:00
|
|
|
|
2015-02-03 01:16:32 +03:00
|
|
|
if (position >= 4)
|
|
|
|
{
|
|
|
|
line = string_strnstr(buffer, "\r\n\r\n", position);
|
|
|
|
|
|
|
|
if (line)
|
|
|
|
{
|
|
|
|
payloadOffset = (line - buffer) + 4;
|
|
|
|
payload = &buffer[payloadOffset];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2015-02-02 02:50:21 +03:00
|
|
|
|
2015-02-03 01:16:32 +03:00
|
|
|
if (payloadOffset)
|
|
|
|
{
|
2012-04-17 00:21:46 +04:00
|
|
|
count = 0;
|
2015-02-03 01:16:32 +03:00
|
|
|
line = buffer;
|
|
|
|
|
|
|
|
position = Stream_GetPosition(s);
|
2012-04-17 00:21:46 +04:00
|
|
|
|
2015-02-03 01:16:32 +03:00
|
|
|
while ((line = string_strnstr(line, "\r\n", payloadOffset - (line - buffer) - 2)))
|
2012-04-17 00:21:46 +04:00
|
|
|
{
|
2015-02-03 01:16:32 +03:00
|
|
|
line += 2;
|
2012-04-17 00:21:46 +04:00
|
|
|
count++;
|
|
|
|
}
|
|
|
|
|
2015-02-02 04:47:43 +03:00
|
|
|
response->count = count;
|
2014-08-18 19:22:43 +04:00
|
|
|
|
2013-08-30 16:19:50 +04:00
|
|
|
if (count)
|
|
|
|
{
|
2015-02-02 04:47:43 +03:00
|
|
|
response->lines = (char**) calloc(response->count, sizeof(char*));
|
2014-08-18 19:22:43 +04:00
|
|
|
|
2015-02-02 04:47:43 +03:00
|
|
|
if (!response->lines)
|
2014-04-09 17:00:38 +04:00
|
|
|
goto out_error;
|
2013-08-30 16:19:50 +04:00
|
|
|
}
|
2012-04-17 00:21:46 +04:00
|
|
|
|
2015-02-03 01:16:32 +03:00
|
|
|
header = (char*) malloc(payloadOffset);
|
|
|
|
|
|
|
|
if (!header)
|
|
|
|
goto out_error;
|
|
|
|
|
|
|
|
CopyMemory(header, buffer, payloadOffset);
|
|
|
|
header[payloadOffset - 1] = '\0';
|
|
|
|
header[payloadOffset - 2] = '\0';
|
|
|
|
|
2012-04-17 00:21:46 +04:00
|
|
|
count = 0;
|
2015-02-03 01:16:32 +03:00
|
|
|
line = strtok(header, "\r\n");
|
2012-04-17 00:21:46 +04:00
|
|
|
|
2015-02-02 04:47:43 +03:00
|
|
|
while (line && response->lines)
|
2012-04-17 00:21:46 +04:00
|
|
|
{
|
2015-02-02 04:47:43 +03:00
|
|
|
response->lines[count] = _strdup(line);
|
2014-08-18 19:22:43 +04:00
|
|
|
|
2015-02-02 04:47:43 +03:00
|
|
|
if (!response->lines[count])
|
2014-04-09 17:00:38 +04:00
|
|
|
goto out_error;
|
|
|
|
|
2012-04-17 00:21:46 +04:00
|
|
|
line = strtok(NULL, "\r\n");
|
|
|
|
count++;
|
|
|
|
}
|
|
|
|
|
2015-02-02 04:47:43 +03:00
|
|
|
if (!http_response_parse_header(response))
|
2014-04-09 17:00:38 +04:00
|
|
|
goto out_error;
|
2012-04-17 00:21:46 +04:00
|
|
|
|
2015-02-03 01:16:32 +03:00
|
|
|
response->BodyLength = Stream_GetPosition(s) - payloadOffset;
|
2014-08-18 19:22:43 +04:00
|
|
|
|
2015-02-02 04:47:43 +03:00
|
|
|
if (response->BodyLength > 0)
|
2012-04-17 00:21:46 +04:00
|
|
|
{
|
2015-02-02 04:47:43 +03:00
|
|
|
response->BodyContent = (BYTE*) malloc(response->BodyLength);
|
2014-08-18 19:22:43 +04:00
|
|
|
|
2015-02-02 04:47:43 +03:00
|
|
|
if (!response->BodyContent)
|
2014-04-09 17:00:38 +04:00
|
|
|
goto out_error;
|
2014-05-21 19:32:14 +04:00
|
|
|
|
2015-02-03 01:16:32 +03:00
|
|
|
CopyMemory(response->BodyContent, payload, response->BodyLength);
|
|
|
|
}
|
|
|
|
|
|
|
|
bodyLength = 0; /* expected body length */
|
|
|
|
|
|
|
|
if (response->ContentType)
|
|
|
|
{
|
2015-03-17 23:09:17 +03:00
|
|
|
if (_stricmp(response->ContentType, "application/rpc") != 0)
|
|
|
|
bodyLength = response->ContentLength;
|
|
|
|
else if (_stricmp(response->ContentType, "text/plain") == 0)
|
2015-02-03 01:16:32 +03:00
|
|
|
bodyLength = response->ContentLength;
|
2015-02-25 00:39:00 +03:00
|
|
|
else if (_stricmp(response->ContentType, "text/html") == 0)
|
|
|
|
bodyLength = response->ContentLength;
|
2015-02-03 01:16:32 +03:00
|
|
|
}
|
2015-03-18 16:48:21 +03:00
|
|
|
else
|
|
|
|
{
|
|
|
|
bodyLength = response->BodyLength;
|
|
|
|
}
|
2015-02-03 01:16:32 +03:00
|
|
|
|
|
|
|
if (bodyLength != response->BodyLength)
|
|
|
|
{
|
|
|
|
WLog_WARN(TAG, "http_response_recv: %s unexpected body length: actual: %d, expected: %d",
|
2015-02-25 00:39:00 +03:00
|
|
|
response->ContentType, bodyLength, response->BodyLength);
|
2012-04-17 00:21:46 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
2012-04-19 19:29:53 +04:00
|
|
|
|
2015-02-03 01:16:32 +03:00
|
|
|
if (Stream_GetRemainingLength(s) < 1024)
|
2012-04-19 19:29:53 +04:00
|
|
|
{
|
2015-03-30 18:15:45 +03:00
|
|
|
if (!Stream_EnsureRemainingCapacity(s, 1024))
|
2015-03-26 19:09:47 +03:00
|
|
|
goto out_error;
|
2015-02-03 01:16:32 +03:00
|
|
|
buffer = (char*) Stream_Buffer(s);
|
|
|
|
payload = &buffer[payloadOffset];
|
2012-04-19 19:29:53 +04:00
|
|
|
}
|
2012-04-17 00:21:46 +04:00
|
|
|
}
|
|
|
|
|
2015-06-23 22:29:21 +03:00
|
|
|
free(header);
|
2015-02-03 01:16:32 +03:00
|
|
|
Stream_Free(s, TRUE);
|
2015-02-02 04:47:43 +03:00
|
|
|
return response;
|
2014-04-09 17:00:38 +04:00
|
|
|
out_error:
|
2015-02-02 04:47:43 +03:00
|
|
|
http_response_free(response);
|
2015-06-23 22:29:21 +03:00
|
|
|
free(header);
|
2014-04-09 17:00:38 +04:00
|
|
|
out_free:
|
2015-02-03 01:16:32 +03:00
|
|
|
Stream_Free(s, TRUE);
|
2014-04-09 17:00:38 +04:00
|
|
|
return NULL;
|
2012-04-17 00:21:46 +04:00
|
|
|
}
|
|
|
|
|
2014-08-18 21:34:47 +04:00
|
|
|
HttpResponse* http_response_new()
|
2012-04-17 00:21:46 +04:00
|
|
|
{
|
2015-02-02 04:47:43 +03:00
|
|
|
HttpResponse* response = (HttpResponse*) calloc(1, sizeof(HttpResponse));
|
2014-08-18 19:22:43 +04:00
|
|
|
|
2015-02-02 04:47:43 +03:00
|
|
|
if (!response)
|
2014-04-19 01:08:34 +04:00
|
|
|
return NULL;
|
|
|
|
|
2015-02-02 04:47:43 +03:00
|
|
|
response->Authenticates = ListDictionary_New(FALSE);
|
2015-05-18 12:28:00 +03:00
|
|
|
if (!response->Authenticates)
|
|
|
|
{
|
|
|
|
free(response);
|
|
|
|
return NULL;
|
|
|
|
}
|
2014-12-11 19:25:34 +03:00
|
|
|
|
2015-02-02 04:47:43 +03:00
|
|
|
ListDictionary_KeyObject(response->Authenticates)->fnObjectEquals = strings_equals_nocase;
|
|
|
|
ListDictionary_KeyObject(response->Authenticates)->fnObjectFree = string_free;
|
2014-12-11 19:25:34 +03:00
|
|
|
|
2015-02-02 04:47:43 +03:00
|
|
|
ListDictionary_ValueObject(response->Authenticates)->fnObjectEquals = strings_equals_nocase;
|
|
|
|
ListDictionary_ValueObject(response->Authenticates)->fnObjectFree = string_free;
|
2014-12-11 19:25:34 +03:00
|
|
|
|
2015-02-02 04:47:43 +03:00
|
|
|
return response;
|
2012-04-17 00:21:46 +04:00
|
|
|
}
|
|
|
|
|
2015-02-02 04:47:43 +03:00
|
|
|
void http_response_free(HttpResponse* response)
|
2012-04-17 00:21:46 +04:00
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
2015-02-02 04:47:43 +03:00
|
|
|
if (!response)
|
2014-04-19 01:08:34 +04:00
|
|
|
return;
|
|
|
|
|
2015-05-11 10:07:39 +03:00
|
|
|
if (response->lines)
|
|
|
|
for (i = 0; i < response->count; i++)
|
2015-02-02 04:47:43 +03:00
|
|
|
free(response->lines[i]);
|
2012-04-17 00:21:46 +04:00
|
|
|
|
2015-02-02 04:47:43 +03:00
|
|
|
free(response->lines);
|
|
|
|
free(response->ReasonPhrase);
|
2014-12-11 19:25:34 +03:00
|
|
|
|
2015-02-03 01:16:32 +03:00
|
|
|
free(response->ContentType);
|
|
|
|
|
2015-02-02 04:47:43 +03:00
|
|
|
ListDictionary_Free(response->Authenticates);
|
2012-04-17 00:21:46 +04:00
|
|
|
|
2015-03-17 23:09:17 +03:00
|
|
|
if (response->BodyContent)
|
|
|
|
{
|
2015-02-02 04:47:43 +03:00
|
|
|
free(response->BodyContent);
|
2015-03-17 23:09:17 +03:00
|
|
|
response->BodyContent = NULL;
|
|
|
|
}
|
2012-04-17 00:21:46 +04:00
|
|
|
|
2015-02-02 04:47:43 +03:00
|
|
|
free(response);
|
2012-04-17 00:21:46 +04:00
|
|
|
}
|