Blame lib/html.c

Packit c22fc9
/*
Packit c22fc9
 * Soft:        Perform a GET query to a remote HTTP/HTTPS server.
Packit c22fc9
 *              Set a timer to compute global remote server response
Packit c22fc9
 *              time.
Packit c22fc9
 *
Packit c22fc9
 * Part:        HTML stream parser utility functions.
Packit c22fc9
 *
Packit c22fc9
 * Authors:     Alexandre Cassen, <acassen@linux-vs.org>
Packit c22fc9
 *
Packit c22fc9
 *              This program is distributed in the hope that it will be useful,
Packit c22fc9
 *              but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit c22fc9
 *              MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
Packit c22fc9
 *              See the GNU General Public License for more details.
Packit c22fc9
 *
Packit c22fc9
 *              This program is free software; you can redistribute it and/or
Packit c22fc9
 *              modify it under the terms of the GNU General Public License
Packit c22fc9
 *              as published by the Free Software Foundation; either version
Packit c22fc9
 *              2 of the License, or (at your option) any later version.
Packit c22fc9
 *
Packit c22fc9
 * Copyright (C) 2001-2017 Alexandre Cassen, <acassen@gmail.com>
Packit c22fc9
 */
Packit c22fc9
Packit c22fc9
#include "config.h"
Packit c22fc9
Packit c22fc9
#include <string.h>
Packit c22fc9
#include <stdlib.h>
Packit c22fc9
#include <stdint.h>
Packit c22fc9
Packit c22fc9
#include "html.h"
Packit c22fc9
#include "memory.h"
Packit c22fc9
Packit c22fc9
/* HTTP header tag */
Packit c22fc9
#define CONTENT_LENGTH	"Content-Length:"
Packit c22fc9
Packit c22fc9
/* Return the http header content length */
Packit Service dfccb1
size_t extract_content_length(const char *buffer, size_t size)
Packit c22fc9
{
Packit c22fc9
	char *clen = strstr(buffer, CONTENT_LENGTH);
Packit c22fc9
	size_t len;
Packit c22fc9
	char *end;
Packit c22fc9
Packit c22fc9
	/* Pattern not found */
Packit c22fc9
	if (!clen || clen > buffer + size)
Packit c22fc9
		return SIZE_MAX;
Packit c22fc9
Packit c22fc9
	/* Content-Length extraction */
Packit c22fc9
	len = strtoul(clen + strlen(CONTENT_LENGTH), &end, 10);
Packit c22fc9
	if (*end)
Packit c22fc9
		return SIZE_MAX;
Packit c22fc9
Packit c22fc9
	return len;
Packit c22fc9
}
Packit c22fc9
Packit c22fc9
/*
Packit c22fc9
 * Return the http header error code. According
Packit c22fc9
 * to rfc2616.6.1 status code is between HTTP_Version
Packit c22fc9
 * and Reason_Phrase, separated by space caracter.
Packit c22fc9
 */
Packit Service dfccb1
#include "logger.h"
Packit Service dfccb1
int extract_status_code(const char *buffer, size_t size)
Packit c22fc9
{
Packit Service dfccb1
	const char *buf_end = buffer + size;
Packit Service dfccb1
	char *end;
Packit c22fc9
	unsigned long code;
Packit c22fc9
Packit c22fc9
	/* Status-Code extraction */
Packit Service dfccb1
	while (buffer < buf_end && *buffer != ' ' && *buffer != '\r')
Packit c22fc9
		buffer++;
Packit Service dfccb1
	if (*buffer != ' ')
Packit Service dfccb1
		return 0;
Packit c22fc9
	buffer++;
Packit Service dfccb1
	if (buffer + 3 >= buf_end || *buffer == ' ' || buffer[3] != ' ')
Packit c22fc9
		return 0;
Packit Service dfccb1
	code = strtoul(buffer, &end, 10);	// This line causes a strict-overflow=4 warning with gcc 5.4.0
Packit c22fc9
	if (buffer + 3 != end)
Packit c22fc9
		return 0;
Packit c22fc9
	return code;
Packit c22fc9
}
Packit c22fc9
Packit c22fc9
/* simple function returning a pointer to the html buffer begin */
Packit Service dfccb1
const char * __attribute__ ((pure))
Packit Service dfccb1
extract_html(const char *buffer, size_t size_buffer)
Packit c22fc9
{
Packit Service dfccb1
	const char *end = buffer + size_buffer;
Packit Service dfccb1
	const char *cur;
Packit c22fc9
Packit c22fc9
	for (cur = buffer; cur + 3 < end; cur++)
Packit c22fc9
		if (*cur == '\r' && *(cur+1) == '\n'
Packit c22fc9
		    && *(cur+2) == '\r' && *(cur+3) == '\n')
Packit c22fc9
			return cur + 4;
Packit c22fc9
	return NULL;
Packit c22fc9
}