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
|
|
|
|
2014-09-12 16:36:29 +04:00
|
|
|
|
2014-08-07 18:51:24 +04:00
|
|
|
|
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"
|
|
|
|
|
2014-08-18 19:22:43 +04:00
|
|
|
#define TAG "gateway"
|
|
|
|
|
2014-08-18 21:34:47 +04:00
|
|
|
HttpContext* http_context_new()
|
2012-04-14 22:19:31 +04:00
|
|
|
{
|
2014-08-18 21:34:47 +04:00
|
|
|
return (HttpContext*)calloc(1, sizeof(HttpContext));
|
2012-04-14 22:19:31 +04:00
|
|
|
}
|
|
|
|
|
2014-08-18 21:34:47 +04:00
|
|
|
void http_context_set_method(HttpContext* http_context, char* method)
|
2012-04-14 22:19:31 +04:00
|
|
|
{
|
2012-05-06 08:53:07 +04:00
|
|
|
if (http_context->Method)
|
2012-10-09 07:21:26 +04:00
|
|
|
free(http_context->Method);
|
2012-05-06 08:53:07 +04:00
|
|
|
|
2012-10-09 07:42:01 +04:00
|
|
|
http_context->Method = _strdup(method);
|
2014-04-09 17:00:38 +04:00
|
|
|
// TODO: check result
|
2012-04-14 22:19:31 +04:00
|
|
|
}
|
|
|
|
|
2014-08-18 21:34:47 +04:00
|
|
|
void http_context_set_uri(HttpContext* http_context, char* uri)
|
2012-04-14 22:19:31 +04:00
|
|
|
{
|
2012-05-06 08:53:07 +04:00
|
|
|
if (http_context->URI)
|
2012-10-09 07:21:26 +04:00
|
|
|
free(http_context->URI);
|
2012-05-06 08:53:07 +04:00
|
|
|
|
2012-10-09 07:42:01 +04:00
|
|
|
http_context->URI = _strdup(uri);
|
2014-04-09 17:00:38 +04:00
|
|
|
// TODO: check result
|
2012-04-14 22:19:31 +04:00
|
|
|
}
|
|
|
|
|
2014-08-18 21:34:47 +04:00
|
|
|
void http_context_set_user_agent(HttpContext* http_context, char* user_agent)
|
2012-04-14 22:19:31 +04:00
|
|
|
{
|
2012-05-06 08:53:07 +04:00
|
|
|
if (http_context->UserAgent)
|
2012-10-09 07:21:26 +04:00
|
|
|
free(http_context->UserAgent);
|
2012-05-06 08:53:07 +04:00
|
|
|
|
2012-10-09 07:42:01 +04:00
|
|
|
http_context->UserAgent = _strdup(user_agent);
|
2014-04-09 17:00:38 +04:00
|
|
|
// TODO: check result
|
2012-04-14 22:19:31 +04:00
|
|
|
}
|
|
|
|
|
2014-08-18 21:34:47 +04:00
|
|
|
void http_context_set_host(HttpContext* http_context, char* host)
|
2012-04-14 22:19:31 +04:00
|
|
|
{
|
2012-05-06 08:53:07 +04:00
|
|
|
if (http_context->Host)
|
2012-10-09 07:21:26 +04:00
|
|
|
free(http_context->Host);
|
2012-05-06 08:53:07 +04:00
|
|
|
|
2012-10-09 07:42:01 +04:00
|
|
|
http_context->Host = _strdup(host);
|
2014-04-09 17:00:38 +04:00
|
|
|
// TODO: check result
|
2012-04-14 22:19:31 +04:00
|
|
|
}
|
|
|
|
|
2014-08-18 21:34:47 +04:00
|
|
|
void http_context_set_accept(HttpContext* http_context, char* accept)
|
2012-04-14 22:19:31 +04:00
|
|
|
{
|
2012-05-06 08:53:07 +04:00
|
|
|
if (http_context->Accept)
|
2012-10-09 07:21:26 +04:00
|
|
|
free(http_context->Accept);
|
2012-05-06 08:53:07 +04:00
|
|
|
|
2012-10-09 07:42:01 +04:00
|
|
|
http_context->Accept = _strdup(accept);
|
2014-04-09 17:00:38 +04:00
|
|
|
// TODO: check result
|
2012-04-14 22:19:31 +04:00
|
|
|
}
|
|
|
|
|
2014-08-18 21:34:47 +04:00
|
|
|
void http_context_set_cache_control(HttpContext* http_context, char* cache_control)
|
2012-04-14 22:19:31 +04:00
|
|
|
{
|
2012-05-06 08:53:07 +04:00
|
|
|
if (http_context->CacheControl)
|
2012-10-09 07:21:26 +04:00
|
|
|
free(http_context->CacheControl);
|
2012-05-06 08:53:07 +04:00
|
|
|
|
2012-10-09 07:42:01 +04:00
|
|
|
http_context->CacheControl = _strdup(cache_control);
|
2014-04-09 17:00:38 +04:00
|
|
|
// TODO: check result
|
2012-04-14 22:19:31 +04:00
|
|
|
}
|
|
|
|
|
2014-08-18 21:34:47 +04:00
|
|
|
void http_context_set_connection(HttpContext* http_context, char* connection)
|
2012-04-14 22:19:31 +04:00
|
|
|
{
|
2012-05-06 08:53:07 +04:00
|
|
|
if (http_context->Connection)
|
2012-10-09 07:21:26 +04:00
|
|
|
free(http_context->Connection);
|
2012-05-06 08:53:07 +04:00
|
|
|
|
2012-10-09 07:42:01 +04:00
|
|
|
http_context->Connection = _strdup(connection);
|
2014-04-09 17:00:38 +04:00
|
|
|
// TODO: check result
|
2012-04-14 22:19:31 +04:00
|
|
|
}
|
|
|
|
|
2014-08-18 21:34:47 +04:00
|
|
|
void http_context_set_pragma(HttpContext* http_context, char* pragma)
|
2012-04-14 22:19:31 +04:00
|
|
|
{
|
2012-05-06 08:53:07 +04:00
|
|
|
if (http_context->Pragma)
|
2012-10-09 07:21:26 +04:00
|
|
|
free(http_context->Pragma);
|
2012-05-06 08:53:07 +04:00
|
|
|
|
2012-10-09 07:42:01 +04:00
|
|
|
http_context->Pragma = _strdup(pragma);
|
2014-04-09 17:00:38 +04:00
|
|
|
// TODO: check result
|
2012-04-14 22:19:31 +04:00
|
|
|
}
|
|
|
|
|
2014-08-18 21:34:47 +04:00
|
|
|
void http_context_free(HttpContext* http_context)
|
2012-04-14 22:19:31 +04:00
|
|
|
{
|
|
|
|
if (http_context != NULL)
|
|
|
|
{
|
2012-10-09 07:21:26 +04:00
|
|
|
free(http_context->UserAgent);
|
|
|
|
free(http_context->Host);
|
2012-12-12 09:49:15 +04:00
|
|
|
free(http_context->URI);
|
2012-10-09 07:21:26 +04:00
|
|
|
free(http_context->Accept);
|
2012-12-12 09:49:15 +04:00
|
|
|
free(http_context->Method);
|
2012-10-09 07:21:26 +04:00
|
|
|
free(http_context->CacheControl);
|
|
|
|
free(http_context->Connection);
|
|
|
|
free(http_context->Pragma);
|
|
|
|
free(http_context);
|
2012-04-14 22:19:31 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-08-18 21:34:47 +04:00
|
|
|
void http_request_set_method(HttpRequest* http_request, char* method)
|
2012-04-14 22:19:31 +04:00
|
|
|
{
|
2012-05-06 08:53:07 +04:00
|
|
|
if (http_request->Method)
|
2012-10-09 07:21:26 +04:00
|
|
|
free(http_request->Method);
|
2012-05-06 08:53:07 +04:00
|
|
|
|
2012-10-09 07:42:01 +04:00
|
|
|
http_request->Method = _strdup(method);
|
2014-04-09 17:00:38 +04:00
|
|
|
// TODO: check result
|
2012-04-14 22:19:31 +04:00
|
|
|
}
|
|
|
|
|
2014-08-18 21:34:47 +04:00
|
|
|
void http_request_set_uri(HttpRequest* http_request, char* uri)
|
2012-04-14 22:19:31 +04:00
|
|
|
{
|
2012-05-06 08:53:07 +04:00
|
|
|
if (http_request->URI)
|
2012-10-09 07:21:26 +04:00
|
|
|
free(http_request->URI);
|
2012-05-06 08:53:07 +04:00
|
|
|
|
2012-10-09 07:42:01 +04:00
|
|
|
http_request->URI = _strdup(uri);
|
2014-04-09 17:00:38 +04:00
|
|
|
// TODO: check result
|
2012-04-14 22:19:31 +04:00
|
|
|
}
|
|
|
|
|
2014-08-18 21:34:47 +04:00
|
|
|
void http_request_set_auth_scheme(HttpRequest* http_request, char* auth_scheme)
|
2012-04-17 00:21:46 +04:00
|
|
|
{
|
2012-05-06 08:53:07 +04:00
|
|
|
if (http_request->AuthScheme)
|
2012-10-09 07:21:26 +04:00
|
|
|
free(http_request->AuthScheme);
|
2012-05-06 08:53:07 +04:00
|
|
|
|
2012-10-09 07:42:01 +04:00
|
|
|
http_request->AuthScheme = _strdup(auth_scheme);
|
2014-04-09 17:00:38 +04:00
|
|
|
// TODO: check result
|
2012-04-17 00:21:46 +04:00
|
|
|
}
|
|
|
|
|
2014-08-18 21:34:47 +04:00
|
|
|
void http_request_set_auth_param(HttpRequest* http_request, char* auth_param)
|
2012-04-17 00:21:46 +04:00
|
|
|
{
|
2012-05-06 08:53:07 +04:00
|
|
|
if (http_request->AuthParam)
|
2012-10-09 07:21:26 +04:00
|
|
|
free(http_request->AuthParam);
|
2012-05-06 08:53:07 +04:00
|
|
|
|
2012-10-09 07:42:01 +04:00
|
|
|
http_request->AuthParam = _strdup(auth_param);
|
2014-04-09 17:00:38 +04:00
|
|
|
// TODO: check result
|
2012-04-17 00:21:46 +04:00
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
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];
|
|
|
|
_itoa_s(ContentLength, str, sizeof(str), 10);
|
|
|
|
length = strlen("Content-Length") + strlen(str) + 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, "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;
|
|
|
|
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;
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2014-08-18 21:34:47 +04:00
|
|
|
wStream* http_request_write(HttpContext* http_context, HttpRequest* http_request)
|
2012-04-14 22:19:31 +04:00
|
|
|
{
|
2014-04-09 17:00:38 +04:00
|
|
|
int i, count;
|
2014-08-18 21:34:47 +04:00
|
|
|
char** lines;
|
|
|
|
wStream* s;
|
2012-04-14 22:19:31 +04:00
|
|
|
int length = 0;
|
2014-04-09 17:00:38 +04:00
|
|
|
count = 9;
|
2014-08-18 21:34:47 +04:00
|
|
|
lines = (char**)calloc(count, sizeof(char*));
|
2014-08-18 19:22:43 +04:00
|
|
|
|
2014-04-09 17:00:38 +04:00
|
|
|
if (!lines)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
lines[0] = http_encode_header_line(http_request->Method, http_request->URI);
|
|
|
|
lines[1] = http_encode_body_line("Cache-Control", http_context->CacheControl);
|
|
|
|
lines[2] = http_encode_body_line("Connection", http_context->Connection);
|
|
|
|
lines[3] = http_encode_body_line("Pragma", http_context->Pragma);
|
|
|
|
lines[4] = http_encode_body_line("Accept", http_context->Accept);
|
|
|
|
lines[5] = http_encode_body_line("User-Agent", http_context->UserAgent);
|
|
|
|
lines[6] = http_encode_content_length_line(http_request->ContentLength);
|
|
|
|
lines[7] = http_encode_body_line("Host", http_context->Host);
|
|
|
|
|
|
|
|
/* check that everything went well */
|
|
|
|
for (i = 0; i < 8; i++)
|
|
|
|
{
|
|
|
|
if (!lines[i])
|
|
|
|
goto out_free;
|
|
|
|
}
|
2012-04-17 00:21:46 +04:00
|
|
|
|
|
|
|
if (http_request->Authorization != NULL)
|
|
|
|
{
|
2014-04-09 17:00:38 +04:00
|
|
|
lines[8] = http_encode_body_line("Authorization", http_request->Authorization);
|
2014-08-18 19:22:43 +04:00
|
|
|
|
2014-04-09 17:00:38 +04:00
|
|
|
if (!lines[8])
|
|
|
|
goto out_free;
|
2012-04-17 00:21:46 +04:00
|
|
|
}
|
|
|
|
else if ((http_request->AuthScheme != NULL) && (http_request->AuthParam != NULL))
|
|
|
|
{
|
2014-04-09 17:00:38 +04:00
|
|
|
lines[8] = http_encode_authorization_line(http_request->AuthScheme, http_request->AuthParam);
|
2014-08-18 19:22:43 +04:00
|
|
|
|
2014-04-09 17:00:38 +04:00
|
|
|
if (!lines[8])
|
|
|
|
goto out_free;
|
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 */
|
2013-04-30 06:35:15 +04:00
|
|
|
Stream_Length(s) = Stream_GetPosition(s);
|
2012-04-14 22:19:31 +04:00
|
|
|
return s;
|
2014-04-09 17:00:38 +04:00
|
|
|
out_free:
|
2014-08-18 19:22:43 +04:00
|
|
|
|
2014-04-09 17:00:38 +04:00
|
|
|
for (i = 0; i < 9; i++)
|
2012-04-14 22:19:31 +04:00
|
|
|
{
|
2014-04-09 17:00:38 +04:00
|
|
|
if (lines[i])
|
|
|
|
free(lines[i]);
|
2012-04-14 22:19:31 +04:00
|
|
|
}
|
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
|
|
|
}
|
|
|
|
|
2014-08-18 21:34:47 +04:00
|
|
|
void http_request_free(HttpRequest* http_request)
|
2012-04-14 22:19:31 +04:00
|
|
|
{
|
2014-04-09 17:00:38 +04:00
|
|
|
if (!http_request)
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (http_request->AuthParam)
|
2012-12-12 08:34:51 +04:00
|
|
|
free(http_request->AuthParam);
|
2014-08-18 19:22:43 +04:00
|
|
|
|
2014-04-09 17:00:38 +04:00
|
|
|
if (http_request->AuthScheme)
|
2012-12-12 08:34:51 +04:00
|
|
|
free(http_request->AuthScheme);
|
2014-08-18 19:22:43 +04:00
|
|
|
|
2014-04-09 17:00:38 +04:00
|
|
|
if (http_request->Authorization)
|
2012-12-12 08:34:51 +04:00
|
|
|
free(http_request->Authorization);
|
2014-08-18 19:22:43 +04:00
|
|
|
|
2014-04-09 17:00:38 +04:00
|
|
|
free(http_request->Content);
|
|
|
|
free(http_request->Method);
|
|
|
|
free(http_request->URI);
|
|
|
|
free(http_request);
|
2012-04-14 22:19:31 +04:00
|
|
|
}
|
2012-04-17 00:21:46 +04:00
|
|
|
|
2014-08-18 21:34:47 +04:00
|
|
|
BOOL http_response_parse_header_status_line(HttpResponse* http_response, char* status_line)
|
2012-04-17 00:21:46 +04:00
|
|
|
{
|
2014-08-18 21:34:47 +04:00
|
|
|
char* separator;
|
|
|
|
char* status_code;
|
|
|
|
char* reason_phrase;
|
2012-04-17 00:21:46 +04:00
|
|
|
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';
|
|
|
|
http_response->StatusCode = atoi(status_code);
|
2012-10-09 07:42:01 +04:00
|
|
|
http_response->ReasonPhrase = _strdup(reason_phrase);
|
2014-08-18 19:22:43 +04:00
|
|
|
|
2014-04-09 17:00:38 +04:00
|
|
|
if (!http_response->ReasonPhrase)
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2014-08-18 21:34:47 +04:00
|
|
|
BOOL http_response_parse_header_field(HttpResponse* http_response, char* name, char* value)
|
2012-04-17 00:21:46 +04:00
|
|
|
{
|
2013-05-07 00:57:39 +04:00
|
|
|
if (_stricmp(name, "Content-Length") == 0)
|
2012-04-17 00:21:46 +04:00
|
|
|
{
|
|
|
|
http_response->ContentLength = atoi(value);
|
|
|
|
}
|
2014-04-19 01:08:34 +04:00
|
|
|
else if (_stricmp(name, "WWW-Authenticate") == 0)
|
2012-04-17 00:21:46 +04:00
|
|
|
{
|
2014-08-18 21:34:47 +04:00
|
|
|
char* separator;
|
|
|
|
char* authScheme, *authValue;
|
2012-04-17 00:21:46 +04:00
|
|
|
separator = strchr(value, ' ');
|
|
|
|
|
|
|
|
if (separator != NULL)
|
|
|
|
{
|
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
|
|
|
}
|
|
|
|
|
2014-04-19 01:08:34 +04:00
|
|
|
return ListDictionary_Add(http_response->Authenticates, authScheme, authValue);
|
2012-04-17 00:21:46 +04:00
|
|
|
}
|
2014-08-18 19:22:43 +04:00
|
|
|
|
2014-04-09 17:00:38 +04:00
|
|
|
return TRUE;
|
2012-04-17 00:21:46 +04:00
|
|
|
}
|
|
|
|
|
2014-08-18 21:34:47 +04:00
|
|
|
BOOL http_response_parse_header(HttpResponse* http_response)
|
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;
|
|
|
|
char c;
|
2012-04-17 00:21:46 +04:00
|
|
|
|
2013-08-29 17:30:22 +04:00
|
|
|
if (!http_response)
|
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
if (!http_response->lines)
|
|
|
|
return FALSE;
|
|
|
|
|
2013-05-07 00:57:39 +04:00
|
|
|
if (!http_response_parse_header_status_line(http_response, http_response->lines[0]))
|
|
|
|
return FALSE;
|
2012-04-17 00:21:46 +04:00
|
|
|
|
|
|
|
for (count = 1; count < http_response->count; count++)
|
|
|
|
{
|
|
|
|
line = http_response->lines[count];
|
2013-05-07 00:57:39 +04:00
|
|
|
/**
|
|
|
|
* name end_of_header
|
|
|
|
* | |
|
|
|
|
* v v
|
|
|
|
* <header name> : <header value>
|
|
|
|
* ^ ^
|
|
|
|
* | |
|
|
|
|
* colon_pos value
|
|
|
|
*/
|
|
|
|
colon_pos = strchr(line, ':');
|
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
|
|
|
|
2014-04-09 17:00:38 +04:00
|
|
|
if (!http_response_parse_header_field(http_response, name, value))
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2014-08-18 21:34:47 +04:00
|
|
|
void http_response_print(HttpResponse* http_response)
|
2012-04-22 00:18:07 +04:00
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
|
|
|
for (i = 0; i < http_response->count; i++)
|
|
|
|
{
|
2014-09-12 16:36:29 +04:00
|
|
|
WLog_ERR(TAG, "%s", http_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
|
|
|
{
|
2014-08-18 21:34:47 +04:00
|
|
|
BYTE* p;
|
2012-04-17 00:21:46 +04:00
|
|
|
int nbytes;
|
|
|
|
int length;
|
|
|
|
int status;
|
2014-08-18 21:34:47 +04:00
|
|
|
BYTE* buffer;
|
|
|
|
char* content;
|
|
|
|
char* header_end;
|
|
|
|
HttpResponse* http_response;
|
2012-04-17 00:21:46 +04:00
|
|
|
nbytes = 0;
|
2012-05-06 08:53:07 +04:00
|
|
|
length = 10000;
|
2012-12-12 08:34:51 +04:00
|
|
|
content = NULL;
|
2014-05-21 19:32:14 +04:00
|
|
|
buffer = calloc(length, 1);
|
2014-08-18 19:22:43 +04:00
|
|
|
|
2014-04-09 17:00:38 +04:00
|
|
|
if (!buffer)
|
|
|
|
return NULL;
|
|
|
|
|
2012-04-17 00:21:46 +04:00
|
|
|
http_response = http_response_new();
|
2014-08-18 19:22:43 +04:00
|
|
|
|
2014-04-09 17:00:38 +04:00
|
|
|
if (!http_response)
|
|
|
|
goto out_free;
|
2012-04-17 00:21:46 +04:00
|
|
|
|
|
|
|
p = buffer;
|
2012-12-12 08:34:51 +04:00
|
|
|
http_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
|
|
|
{
|
2012-11-17 12:45:15 +04:00
|
|
|
while (nbytes < 5)
|
|
|
|
{
|
2014-05-21 19:32:14 +04:00
|
|
|
status = BIO_read(tls->bio, p, length - nbytes);
|
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
|
|
|
|
VALGRIND_MAKE_MEM_DEFINED(p, status);
|
|
|
|
#endif
|
2014-04-09 17:00:38 +04:00
|
|
|
nbytes += status;
|
2014-08-18 21:34:47 +04:00
|
|
|
p = (BYTE*) &buffer[nbytes];
|
2012-11-17 12:45:15 +04:00
|
|
|
}
|
2012-04-17 00:21:46 +04:00
|
|
|
|
2014-08-18 21:34:47 +04:00
|
|
|
header_end = strstr((char*) buffer, "\r\n\r\n");
|
2014-08-18 19:22:43 +04:00
|
|
|
|
2013-05-07 00:57:39 +04:00
|
|
|
if (!header_end)
|
2012-11-17 12:45:15 +04:00
|
|
|
{
|
2014-09-12 16:36:29 +04:00
|
|
|
WLog_ERR(TAG, "invalid response:");
|
2014-08-18 19:22:43 +04:00
|
|
|
winpr_HexDump(TAG, WLOG_ERROR, buffer, status);
|
2014-04-09 17:00:38 +04:00
|
|
|
goto out_error;
|
2012-11-17 12:45:15 +04:00
|
|
|
}
|
2012-04-17 00:21:46 +04:00
|
|
|
|
2013-05-07 00:57:39 +04:00
|
|
|
header_end += 2;
|
|
|
|
|
2012-04-17 00:21:46 +04:00
|
|
|
if (header_end != NULL)
|
|
|
|
{
|
|
|
|
int count;
|
2014-08-18 21:34:47 +04:00
|
|
|
char* line;
|
2012-04-17 00:21:46 +04:00
|
|
|
header_end[0] = '\0';
|
|
|
|
header_end[1] = '\0';
|
2014-05-21 19:32:14 +04:00
|
|
|
content = header_end + 2;
|
2012-04-17 00:21:46 +04:00
|
|
|
count = 0;
|
2014-08-18 21:34:47 +04:00
|
|
|
line = (char*) buffer;
|
2012-04-17 00:21:46 +04:00
|
|
|
|
|
|
|
while ((line = strstr(line, "\r\n")) != NULL)
|
|
|
|
{
|
|
|
|
line++;
|
|
|
|
count++;
|
|
|
|
}
|
|
|
|
|
|
|
|
http_response->count = count;
|
2014-08-18 19:22:43 +04:00
|
|
|
|
2013-08-30 16:19:50 +04:00
|
|
|
if (count)
|
|
|
|
{
|
2014-08-18 21:34:47 +04:00
|
|
|
http_response->lines = (char**)calloc(http_response->count, sizeof(char*));
|
2014-08-18 19:22:43 +04:00
|
|
|
|
2014-04-09 17:00:38 +04:00
|
|
|
if (!http_response->lines)
|
|
|
|
goto out_error;
|
2013-08-30 16:19:50 +04:00
|
|
|
}
|
2012-04-17 00:21:46 +04:00
|
|
|
|
|
|
|
count = 0;
|
2014-08-18 21:34:47 +04:00
|
|
|
line = strtok((char*) buffer, "\r\n");
|
2012-04-17 00:21:46 +04:00
|
|
|
|
|
|
|
while (line != NULL)
|
|
|
|
{
|
2012-10-09 07:42:01 +04:00
|
|
|
http_response->lines[count] = _strdup(line);
|
2014-08-18 19:22:43 +04:00
|
|
|
|
2014-04-09 17:00:38 +04:00
|
|
|
if (!http_response->lines[count])
|
|
|
|
goto out_error;
|
|
|
|
|
2012-04-17 00:21:46 +04:00
|
|
|
line = strtok(NULL, "\r\n");
|
|
|
|
count++;
|
|
|
|
}
|
|
|
|
|
2013-05-07 00:57:39 +04:00
|
|
|
if (!http_response_parse_header(http_response))
|
2014-04-09 17:00:38 +04:00
|
|
|
goto out_error;
|
2012-04-17 00:21:46 +04:00
|
|
|
|
2014-08-18 21:34:47 +04:00
|
|
|
http_response->bodyLen = nbytes - (content - (char*)buffer);
|
2014-08-18 19:22:43 +04:00
|
|
|
|
2014-05-21 19:32:14 +04:00
|
|
|
if (http_response->bodyLen > 0)
|
2012-04-17 00:21:46 +04:00
|
|
|
{
|
2014-08-18 21:34:47 +04:00
|
|
|
http_response->BodyContent = (BYTE*)malloc(http_response->bodyLen);
|
2014-08-18 19:22:43 +04:00
|
|
|
|
2014-05-21 19:32:14 +04:00
|
|
|
if (!http_response->BodyContent)
|
2014-04-09 17:00:38 +04:00
|
|
|
goto out_error;
|
2014-05-21 19:32:14 +04:00
|
|
|
|
|
|
|
CopyMemory(http_response->BodyContent, content, http_response->bodyLen);
|
2012-04-17 00:21:46 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
2012-04-19 19:29:53 +04:00
|
|
|
|
|
|
|
if ((length - nbytes) <= 0)
|
|
|
|
{
|
|
|
|
length *= 2;
|
2012-10-09 07:21:26 +04:00
|
|
|
buffer = realloc(buffer, length);
|
2014-08-18 21:34:47 +04:00
|
|
|
p = (BYTE*) &buffer[nbytes];
|
2012-04-19 19:29:53 +04:00
|
|
|
}
|
2012-04-17 00:21:46 +04:00
|
|
|
}
|
|
|
|
|
2012-10-09 07:21:26 +04:00
|
|
|
free(buffer);
|
2012-04-17 00:21:46 +04:00
|
|
|
return http_response;
|
2014-04-09 17:00:38 +04:00
|
|
|
out_error:
|
|
|
|
http_response_free(http_response);
|
|
|
|
out_free:
|
|
|
|
free(buffer);
|
|
|
|
return NULL;
|
2012-04-17 00:21:46 +04:00
|
|
|
}
|
|
|
|
|
2014-08-18 21:34:47 +04:00
|
|
|
static BOOL strings_equals_nocase(void* obj1, void* obj2)
|
2014-04-19 01:08:34 +04:00
|
|
|
{
|
|
|
|
if (!obj1 || !obj2)
|
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
return _stricmp(obj1, obj2) == 0;
|
|
|
|
}
|
|
|
|
|
2014-08-18 21:34:47 +04:00
|
|
|
static void string_free(void* obj1)
|
2014-04-19 01:08:34 +04:00
|
|
|
{
|
|
|
|
if (!obj1)
|
|
|
|
return;
|
2014-08-18 19:22:43 +04:00
|
|
|
|
2014-04-19 01:08:34 +04:00
|
|
|
free(obj1);
|
|
|
|
}
|
|
|
|
|
2014-08-18 21:34:47 +04:00
|
|
|
HttpResponse* http_response_new()
|
2012-04-17 00:21:46 +04:00
|
|
|
{
|
2014-08-18 21:34:47 +04:00
|
|
|
HttpResponse* ret = (HttpResponse*)calloc(1, sizeof(HttpResponse));
|
2014-08-18 19:22:43 +04:00
|
|
|
|
2014-04-19 01:08:34 +04:00
|
|
|
if (!ret)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
ret->Authenticates = ListDictionary_New(FALSE);
|
|
|
|
ListDictionary_KeyObject(ret->Authenticates)->fnObjectEquals = strings_equals_nocase;
|
|
|
|
ListDictionary_KeyObject(ret->Authenticates)->fnObjectFree = string_free;
|
|
|
|
ListDictionary_ValueObject(ret->Authenticates)->fnObjectEquals = strings_equals_nocase;
|
|
|
|
ListDictionary_ValueObject(ret->Authenticates)->fnObjectFree = string_free;
|
|
|
|
return ret;
|
2012-04-17 00:21:46 +04:00
|
|
|
}
|
|
|
|
|
2014-08-18 21:34:47 +04:00
|
|
|
void http_response_free(HttpResponse* http_response)
|
2012-04-17 00:21:46 +04:00
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
2014-04-19 01:08:34 +04:00
|
|
|
if (!http_response)
|
|
|
|
return;
|
|
|
|
|
|
|
|
for (i = 0; i < http_response->count; i++)
|
|
|
|
free(http_response->lines[i]);
|
2012-04-17 00:21:46 +04:00
|
|
|
|
2014-04-19 01:08:34 +04:00
|
|
|
free(http_response->lines);
|
|
|
|
free(http_response->ReasonPhrase);
|
|
|
|
ListDictionary_Free(http_response->Authenticates);
|
2012-04-17 00:21:46 +04:00
|
|
|
|
2014-04-19 01:08:34 +04:00
|
|
|
if (http_response->ContentLength > 0)
|
2014-05-21 19:32:14 +04:00
|
|
|
free(http_response->BodyContent);
|
2012-04-17 00:21:46 +04:00
|
|
|
|
2014-04-19 01:08:34 +04:00
|
|
|
free(http_response);
|
2012-04-17 00:21:46 +04:00
|
|
|
}
|