src/server/proxy/httpclient.c

changeset 665
b8d5b797d090
parent 662
70fdf948b642
child 666
c99e0b352e36
equal deleted inserted replaced
664:9b1066313c17 665:b8d5b797d090
1 /* 1 /*
2 * Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license 2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
3 * Click nbfs://nbhost/SystemFileSystem/Templates/cFiles/file.c to edit this template 3 *
4 * Copyright 2026 Olaf Wintermann. All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions are met:
8 *
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 *
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
17 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
20 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26 * POSSIBILITY OF SUCH DAMAGE.
4 */ 27 */
5 28
29 #include "httpclient.h"
30
31 HttpClient* http_client_new(EventHandler *ev) {
32 CxMempool *mp = cxMempoolCreate(32, CX_MEMPOOL_TYPE_PURE);
33 if(!mp) {
34 return NULL;
35 }
36
37 HttpClient *client = malloc(sizeof(HttpClient));
38 HeaderArray *req_headers = header_array_create();
39 HeaderArray *resp_headers = header_array_create();
40 if(!client || !req_headers || !resp_headers) {
41 free(client);
42 header_array_free(req_headers);
43 header_array_free(resp_headers);
44 cxMempoolFree(mp);
45 return NULL;
46 }
47
48 memset(client, 0, sizeof(HttpClient));
49 client->request_headers = req_headers;
50 client->response_headers = resp_headers;
51
52 return client;
53 }
54
55 int http_client_set_addr(HttpClient *client, const struct sockaddr *addr, socklen_t addrlen) {
56 free(client->addr);
57 client->addr = NULL;
58 client->addrlen = 0;
59
60 void *newaddr = malloc(addrlen);
61 if(!newaddr) {
62 return 1;
63 }
64 memcpy(newaddr, addr, addrlen);
65 client->addr = newaddr;
66 client->addrlen = addrlen;
67
68 return 0;
69 }
70
71 int http_client_add_request_header(HttpClient *client, cxmutstr name, cxmutstr value) {
72 return header_array_add(client->request_headers, name, value);
73 }
74
75 int http_client_add_request_header_copy(HttpClient *client, cxstring name, cxstring value) {
76 cxmutstr n = cx_strdup_a(client->mp->allocator, name);
77 cxmutstr v = cx_strdup_a(client->mp->allocator, value);
78
79 int err = 1;
80 if(n.ptr && v.ptr) {
81 err = http_client_add_request_header(client, n, v);
82 }
83 if(err) {
84 cxFree(client->mp->allocator, n.ptr);
85 cxFree(client->mp->allocator, v.ptr);
86 }
87 return err;
88 }

mercurial