2023-03-07 23:10:06 +00:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2022-2023 Jordan Bancino <@jordan:bancino.net>
|
|
|
|
*
|
|
|
|
* Permission is hereby granted, free of charge, to any person
|
|
|
|
* obtaining a copy of this software and associated documentation files
|
|
|
|
* (the "Software"), to deal in the Software without restriction,
|
|
|
|
* including without limitation the rights to use, copy, modify, merge,
|
|
|
|
* publish, distribute, sublicense, and/or sell copies of the Software,
|
|
|
|
* and to permit persons to whom the Software is furnished to do so,
|
|
|
|
* subject to the following conditions:
|
|
|
|
*
|
|
|
|
* The above copyright notice and this permission notice shall be
|
|
|
|
* included in all copies or portions of the Software.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
|
|
|
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
|
|
|
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
|
|
|
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
|
|
|
|
* BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
|
|
|
|
* ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
|
|
|
|
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
|
|
* SOFTWARE.
|
|
|
|
*/
|
|
|
|
#include <HttpClient.h>
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <ctype.h>
|
|
|
|
|
|
|
|
#include <unistd.h>
|
2023-03-24 00:23:49 +00:00
|
|
|
#include <errno.h>
|
2023-03-07 23:10:06 +00:00
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/socket.h>
|
|
|
|
#include <netdb.h>
|
|
|
|
|
|
|
|
#include <Http.h>
|
|
|
|
#include <Memory.h>
|
|
|
|
#include <Util.h>
|
2023-03-22 00:41:21 +00:00
|
|
|
#include <Tls.h>
|
2023-03-07 23:10:06 +00:00
|
|
|
|
|
|
|
struct HttpClientContext
|
|
|
|
{
|
|
|
|
HashMap *responseHeaders;
|
2023-03-18 14:32:09 +00:00
|
|
|
Stream *stream;
|
2023-03-07 23:10:06 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
HttpClientContext *
|
2023-03-08 01:54:33 +00:00
|
|
|
HttpRequest(HttpRequestMethod method, int flags, unsigned short port, char *host, char *path)
|
2023-03-07 23:10:06 +00:00
|
|
|
{
|
|
|
|
HttpClientContext *context;
|
|
|
|
|
|
|
|
int sd;
|
|
|
|
struct addrinfo hints, *res, *res0;
|
|
|
|
int error;
|
|
|
|
|
2023-03-08 01:54:33 +00:00
|
|
|
char serv[8];
|
|
|
|
|
2023-03-07 23:10:06 +00:00
|
|
|
if (!method || !host || !path)
|
|
|
|
{
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2023-03-22 00:41:21 +00:00
|
|
|
#ifndef TLS_IMPL
|
2023-03-22 01:46:24 +00:00
|
|
|
if (flags & HTTP_FLAG_TLS)
|
2023-03-22 00:41:21 +00:00
|
|
|
{
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2023-03-08 01:54:33 +00:00
|
|
|
if (!port)
|
|
|
|
{
|
2023-03-22 01:46:24 +00:00
|
|
|
if (flags & HTTP_FLAG_TLS)
|
2023-03-08 01:54:33 +00:00
|
|
|
{
|
|
|
|
strcpy(serv, "https");
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
strcpy(serv, "www");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
sprintf(serv, "%hu", port);
|
|
|
|
}
|
|
|
|
|
2023-03-07 23:10:06 +00:00
|
|
|
|
|
|
|
context = Malloc(sizeof(HttpClientContext));
|
|
|
|
if (!context)
|
|
|
|
{
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
memset(&hints, 0, sizeof(hints));
|
|
|
|
hints.ai_family = AF_UNSPEC;
|
|
|
|
hints.ai_socktype = SOCK_STREAM;
|
2023-03-08 01:54:33 +00:00
|
|
|
error = getaddrinfo(host, serv, &hints, &res0);
|
2023-03-07 23:10:06 +00:00
|
|
|
|
|
|
|
if (error)
|
|
|
|
{
|
|
|
|
Free(context);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (res = res0; res; res = res->ai_next)
|
|
|
|
{
|
|
|
|
sd = socket(res->ai_family, res->ai_socktype,
|
|
|
|
res->ai_protocol);
|
|
|
|
|
|
|
|
if (sd < 0)
|
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (connect(sd, res->ai_addr, res->ai_addrlen) < 0)
|
|
|
|
{
|
|
|
|
close(sd);
|
|
|
|
sd = -1;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (sd < 0)
|
|
|
|
{
|
|
|
|
Free(context);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
freeaddrinfo(res0);
|
|
|
|
|
2023-03-22 00:41:21 +00:00
|
|
|
#ifdef TLS_IMPL
|
2023-03-22 01:46:24 +00:00
|
|
|
if (flags & HTTP_FLAG_TLS)
|
2023-03-22 00:41:21 +00:00
|
|
|
{
|
|
|
|
context->stream = TlsClientStream(sd, host);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
context->stream = StreamFd(sd);
|
|
|
|
}
|
|
|
|
#else
|
2023-03-18 14:32:09 +00:00
|
|
|
context->stream = StreamFd(sd);
|
2023-03-22 00:41:21 +00:00
|
|
|
#endif
|
|
|
|
|
2023-03-07 23:10:06 +00:00
|
|
|
if (!context->stream)
|
|
|
|
{
|
|
|
|
Free(context);
|
|
|
|
close(sd);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2023-03-18 14:32:09 +00:00
|
|
|
StreamPrintf(context->stream, "%s %s HTTP/1.1\r\n",
|
|
|
|
HttpRequestMethodToString(method), path);
|
2023-03-07 23:10:06 +00:00
|
|
|
|
|
|
|
HttpRequestHeader(context, "Connection", "close");
|
|
|
|
HttpRequestHeader(context, "User-Agent", "Telodendria/" TELODENDRIA_VERSION);
|
2023-03-08 01:54:33 +00:00
|
|
|
HttpRequestHeader(context, "Host", host);
|
2023-03-07 23:10:06 +00:00
|
|
|
|
|
|
|
return context;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
HttpRequestHeader(HttpClientContext * context, char *key, char *val)
|
|
|
|
{
|
|
|
|
if (!context || !key || !val)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2023-03-18 14:32:09 +00:00
|
|
|
StreamPrintf(context->stream, "%s: %s\r\n", key, val);
|
2023-03-07 23:10:06 +00:00
|
|
|
}
|
|
|
|
|
2023-03-08 02:53:47 +00:00
|
|
|
void
|
|
|
|
HttpRequestSendHeaders(HttpClientContext * context)
|
|
|
|
{
|
|
|
|
if (!context)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2023-03-18 14:32:09 +00:00
|
|
|
StreamPuts(context->stream, "\r\n");
|
|
|
|
StreamFlush(context->stream);
|
2023-03-08 02:53:47 +00:00
|
|
|
}
|
|
|
|
|
2023-03-07 23:10:06 +00:00
|
|
|
HttpStatus
|
|
|
|
HttpRequestSend(HttpClientContext * context)
|
|
|
|
{
|
|
|
|
HttpStatus status;
|
|
|
|
|
|
|
|
char *line = NULL;
|
|
|
|
ssize_t lineLen;
|
|
|
|
size_t lineSize = 0;
|
|
|
|
char *tmp;
|
|
|
|
|
|
|
|
if (!context)
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2023-03-18 14:32:09 +00:00
|
|
|
StreamFlush(context->stream);
|
2023-03-10 18:46:03 +00:00
|
|
|
|
2023-03-07 23:10:06 +00:00
|
|
|
lineLen = UtilGetLine(&line, &lineSize, context->stream);
|
|
|
|
|
2023-03-24 00:23:49 +00:00
|
|
|
while (lineLen == -1 && errno == EAGAIN)
|
|
|
|
{
|
|
|
|
StreamClearError(context->stream);
|
|
|
|
lineLen = UtilGetLine(&line, &lineSize, context->stream);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (lineLen == -1)
|
|
|
|
{
|
|
|
|
Log(LOG_ERR, "HttpRequestSend(): %s", strerror(errno));
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2023-03-07 23:10:06 +00:00
|
|
|
/* Line must contain at least "HTTP/x.x xxx" */
|
|
|
|
if (lineLen < 12)
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!(strncmp(line, "HTTP/1.0", 8) == 0 ||
|
|
|
|
strncmp(line, "HTTP/1.1", 8) == 0))
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
tmp = line + 9;
|
|
|
|
|
2023-03-09 02:46:04 +00:00
|
|
|
while (isspace((unsigned char) *tmp) && *tmp != '\0')
|
2023-03-07 23:10:06 +00:00
|
|
|
{
|
|
|
|
tmp++;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!*tmp)
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
status = atoi(tmp);
|
|
|
|
|
|
|
|
if (!status)
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
context->responseHeaders = HttpParseHeaders(context->stream);
|
|
|
|
if (!context->responseHeaders)
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
return status;
|
|
|
|
}
|
|
|
|
|
|
|
|
HashMap *
|
|
|
|
HttpResponseHeaders(HttpClientContext * context)
|
|
|
|
{
|
|
|
|
if (!context)
|
|
|
|
{
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
return context->responseHeaders;
|
|
|
|
}
|
|
|
|
|
2023-03-18 14:32:09 +00:00
|
|
|
Stream *
|
2023-03-07 23:10:06 +00:00
|
|
|
HttpClientStream(HttpClientContext * context)
|
|
|
|
{
|
|
|
|
if (!context)
|
|
|
|
{
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
return context->stream;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
HttpClientContextFree(HttpClientContext * context)
|
|
|
|
{
|
|
|
|
char *key;
|
|
|
|
void *val;
|
|
|
|
|
|
|
|
if (!context)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
while (HashMapIterate(context->responseHeaders, &key, &val))
|
|
|
|
{
|
|
|
|
Free(val);
|
|
|
|
}
|
|
|
|
|
|
|
|
HashMapFree(context->responseHeaders);
|
|
|
|
|
2023-03-18 14:32:09 +00:00
|
|
|
StreamClose(context->stream);
|
2023-03-07 23:10:06 +00:00
|
|
|
Free(context);
|
|
|
|
}
|