示例#1
0
文件: http.c 项目: Nigel62/FreeRDP
wStream* http_request_write(HttpContext* http_context, HttpRequest* http_request)
{
	int i;
	wStream* s;
	int length = 0;

	http_request->count = 9;
	http_request->lines = (char**) malloc(sizeof(char*) * http_request->count);

	http_request->lines[0] = http_encode_header_line(http_request->Method, http_request->URI);
	http_request->lines[1] = http_encode_body_line("Cache-Control", http_context->CacheControl);
	http_request->lines[2] = http_encode_body_line("Connection", http_context->Connection);
	http_request->lines[3] = http_encode_body_line("Pragma", http_context->Pragma);
	http_request->lines[4] = http_encode_body_line("Accept", http_context->Accept);
	http_request->lines[5] = http_encode_body_line("User-Agent", http_context->UserAgent);
	http_request->lines[6] = http_encode_content_length_line(http_request->ContentLength);
	http_request->lines[7] = http_encode_body_line("Host", http_context->Host);

	if (http_request->Authorization != NULL)
	{
		http_request->lines[8] = http_encode_body_line("Authorization", http_request->Authorization);
	}
	else if ((http_request->AuthScheme != NULL) && (http_request->AuthParam != NULL))
	{
		http_request->lines[8] = http_encode_authorization_line(http_request->AuthScheme, http_request->AuthParam);
	}

	for (i = 0; i < http_request->count; i++)
	{
		length += (strlen(http_request->lines[i]) + 2); /* add +2 for each '\r\n' character */
	}
	length += 2; /* empty line "\r\n" at end of header */
	length += 1; /* null terminator */

	s = Stream_New(NULL, length);

	for (i = 0; i < http_request->count; i++)
	{
		Stream_Write(s, http_request->lines[i], strlen(http_request->lines[i]));
		Stream_Write(s, "\r\n", 2);
		free(http_request->lines[i]);
	}
	Stream_Write(s, "\r\n", 2);

	free(http_request->lines);

	Stream_Write(s, "\0", 1); /* append null terminator */
	Stream_Rewind(s, 1); /* don't include null terminator in length */
	Stream_Length(s) = Stream_GetPosition(s);

	return s;
}
示例#2
0
wStream* http_request_write(HttpContext* http_context, HttpRequest* http_request)
{
    int i, count;
    char** lines;
    wStream* s;
    int length = 0;

    count = 0;
    lines = (char**) calloc(32, sizeof(char*));

    if (!lines)
        return NULL;

    lines[count++] = http_encode_header_line(http_request->Method, http_request->URI);
    lines[count++] = http_encode_body_line("Cache-Control", http_context->CacheControl);
    lines[count++] = http_encode_body_line("Connection", http_context->Connection);
    lines[count++] = http_encode_body_line("Pragma", http_context->Pragma);
    lines[count++] = http_encode_body_line("Accept", http_context->Accept);
    lines[count++] = http_encode_body_line("User-Agent", http_context->UserAgent);
    lines[count++] = http_encode_content_length_line(http_request->ContentLength);
    lines[count++] = http_encode_body_line("Host", http_context->Host);

    /* check that everything went well */
    for (i = 0; i < count; i++)
    {
        if (!lines[i])
            goto out_free;
    }

    if (http_request->Authorization)
    {
        lines[count] = http_encode_body_line("Authorization", http_request->Authorization);

        if (!lines[count])
            goto out_free;

        count++;
    }
    else if (http_request->AuthScheme && http_request->AuthParam)
    {
        lines[count] = http_encode_authorization_line(http_request->AuthScheme, http_request->AuthParam);

        if (!lines[count])
            goto out_free;

        count++;
    }

    for (i = 0; i < count; i++)
    {
        length += (strlen(lines[i]) + 2); /* add +2 for each '\r\n' character */
    }

    length += 2; /* empty line "\r\n" at end of header */
    length += 1; /* null terminator */
    s = Stream_New(NULL, length);

    if (!s)
        goto out_free;

    for (i = 0; i < count; i++)
    {
        Stream_Write(s, lines[i], strlen(lines[i]));
        Stream_Write(s, "\r\n", 2);
        free(lines[i]);
    }

    Stream_Write(s, "\r\n", 2);
    free(lines);
    Stream_Write(s, "\0", 1); /* append null terminator */
    Stream_Rewind(s, 1); /* don't include null terminator in length */
    Stream_Length(s) = Stream_GetPosition(s);
    return s;
out_free:

    for (i = 0; i < 9; i++)
    {
        if (lines[i])
            free(lines[i]);
    }

    free(lines);
    return NULL;
}
示例#3
0
文件: http.c 项目: FreeRDP/FreeRDP
wStream* http_request_write(HttpContext* context, HttpRequest* request)
{
	wStream* s;

	if (!context || !request)
		return NULL;

	s = Stream_New(NULL, 1024);

	if (!s)
		return NULL;

	if (!http_encode_header_line(s, request->Method, request->URI) ||
	    !http_encode_body_line(s, "Cache-Control", context->CacheControl) ||
	    !http_encode_body_line(s, "Connection", context->Connection) ||
	    !http_encode_body_line(s, "Pragma", context->Pragma) ||
	    !http_encode_body_line(s, "Accept", context->Accept) ||
	    !http_encode_body_line(s, "User-Agent", context->UserAgent) ||
	    !http_encode_body_line(s, "Host", context->Host))
		goto fail;

	if (context->RdgConnectionId)
	{
		if (!http_encode_body_line(s, "RDG-Connection-Id", context->RdgConnectionId))
			goto fail;
	}

	if (context->RdgAuthScheme)
	{
		if (!http_encode_body_line(s, "RDG-Auth-Scheme", context->RdgAuthScheme))
			goto fail;
	}

	if (request->TransferEncoding)
	{
		if (!http_encode_body_line(s, "Transfer-Encoding", request->TransferEncoding))
			goto fail;
	}
	else
	{
		if (!http_encode_content_length_line(s, request->ContentLength))
			goto fail;
	}

	if (request->Authorization)
	{
		if (!http_encode_body_line(s, "Authorization", request->Authorization))
			goto fail;
	}
	else if (request->AuthScheme && request->AuthParam)
	{
		if (!http_encode_authorization_line(s, request->AuthScheme, request->AuthParam))
			goto fail;
	}

	Stream_Write(s, "\r\n", 2);
	Stream_SealLength(s);
	return s;
fail:
	Stream_Free(s, TRUE);
	return NULL;
}