nappgui-sys 0.2.0

Rust raw bindings to NAppGUI
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
/*
 * NAppGUI Cross-platform C SDK
 * 2015-2025 Francisco Garcia Collado
 * MIT Licence
 * https://nappgui.com/en/legal/license.html
 *
 * File: httpreq.c
 *
 */

/* HTTP request */

#include "httpreq.h"
#include "oshttpreq.inl"
#include <encode/url.h>
#include <core/arrst.h>
#include <core/heap.h>
#include <core/stream.h>
#include <core/strings.h>
#include <sewer/cassert.h>
#include <sewer/ptr.h>
#include <sewer/unicode.h>

typedef struct _field_t Field;

struct _field_t
{
    String *name;
    String *value;
};

struct _http_t
{
    OSHttp *oshttp;
    String *host_name;
    uint32_t host_ip;
    uint16_t host_port;
    ierror_t error;
    uint32_t rcode;
    String *rprotocol;
    String *rmsg;
    ArrSt(Field) *headers;
};

DeclSt(Field);

/*---------------------------------------------------------------------------*/

static void i_remove_field(Field *field)
{
    cassert_no_null(field);
    str_destroy(&field->name);
    str_destroy(&field->value);
}

/*---------------------------------------------------------------------------*/

void http_destroy(Http **http)
{
    cassert_no_null(http);
    cassert_no_null(*http);
    str_destroy(&(*http)->host_name);
    ptr_destopt(str_destroy, &(*http)->rprotocol, String);
    ptr_destopt(str_destroy, &(*http)->rmsg, String);
    arrst_destroy(&(*http)->headers, i_remove_field, Field);
    oshttp_destroy(&(*http)->oshttp);
    heap_delete(http, Http);
}

/*---------------------------------------------------------------------------*/

static Http *i_create(const char_t *host, const uint16_t port, const bool_t secure)
{
    Http *http = heap_new(Http);
    http->oshttp = oshttp_create(host, port, secure);
    http->host_name = str_c(host);
    http->host_port = port;
    http->error = ENUM_MAX(ierror_t);
    http->rcode = UINT32_MAX;
    http->rmsg = NULL;
    http->rprotocol = NULL;
    http->headers = arrst_create(Field);
    return http;
}

/*---------------------------------------------------------------------------*/

Http *http_create(const char_t *host, const uint16_t port)
{
    return i_create(host, port == UINT16_MAX ? 80 : port, FALSE);
}

/*---------------------------------------------------------------------------*/

Http *http_secure(const char_t *host, const uint16_t port)
{
    return i_create(host, port == UINT16_MAX ? 443 : port, TRUE);
}

/*---------------------------------------------------------------------------*/

void http_clear_headers(Http *http)
{
    cassert_no_null(http);
    oshttp_clear_headers(http->oshttp);
}

/*---------------------------------------------------------------------------*/

void http_add_header(Http *http, const char_t *name, const char_t *value)
{
    cassert_no_null(http);
    oshttp_add_header(http->oshttp, name, value);
}

/*---------------------------------------------------------------------------*/

static const char_t *i_field(ArrSt(Field) *fields, const char_t *name)
{
    arrst_foreach(field, fields, Field)
        if (str_equ_nocase(tc(field->name), name) == TRUE)
            return tc(field->value);
    arrst_end()
    return NULL;
}

/*---------------------------------------------------------------------------*/

static void i_clear_response(Http *http)
{
    cassert_no_null(http);
    http->error = ENUM_MAX(ierror_t);
    http->rcode = UINT32_MAX;
    ptr_destopt(str_destroy, &http->rprotocol, String);
    ptr_destopt(str_destroy, &http->rmsg, String);
    arrst_clear(http->headers, i_remove_field, Field);
}

/*---------------------------------------------------------------------------*/

bool_t http_get(Http *http, const char_t *path, const byte_t *data, const uint32_t size, ierror_t *error)
{
    cassert_no_null(http);
    i_clear_response(http);
    oshttp_get(http->oshttp, path, data, size, TRUE, &http->error);
    ptr_assign(error, http->error);
    return http->error == ekIOK ? TRUE : FALSE;
}

/*---------------------------------------------------------------------------*/

bool_t http_post(Http *http, const char_t *path, const byte_t *data, const uint32_t size, ierror_t *error)
{
    cassert_no_null(http);
    i_clear_response(http);
    oshttp_post(http->oshttp, path, data, size, TRUE, &http->error);
    ptr_assign(error, http->error);
    return http->error == ekIOK ? TRUE : FALSE;
}

/*---------------------------------------------------------------------------*/

static bool_t i_response(Http *http)
{
    cassert_no_null(http);
    if (http->rcode == UINT32_MAX)
    {
        if (http->error == ekIOK)
        {
            Stream *stm = oshttp_response(http->oshttp);
            if (stm != NULL)
            {
                cassert(http->rprotocol == NULL);
                cassert(http->rmsg == NULL);

                stm_lines(line, stm)

                    if (str_empty_c(line) == FALSE)
                    {
                        /* The headers could contain several responses (redirection)
                        We get the last one */
                        if (str_str(line, ":") == NULL)
                        {
                            const char_t *st = line;
                            char_t code[64];
                            uint32_t i = 0;

                            str_destopt(&http->rprotocol);
                            str_destopt(&http->rmsg);

                            while (*line != ' ')
                                line++;
                            http->rprotocol = str_cn(st, (uint32_t)(line - st));

                            line++;
                            while (*line != ' ' && *line != '\0')
                            {
                                code[i] = *line;
                                line++;
                                i++;
                            }

                            code[i] = '\0';

                            http->rcode = str_to_u32(code, 10, NULL);

                            if (*line != '\0')
                            {
                                line++;
                                http->rmsg = str_c(line);
                            }
                            else
                            {
                                http->rmsg = str_c("");
                            }

                            arrst_clear(http->headers, i_remove_field, Field);
                        }
                        else
                        {
                            Field *header = arrst_new(http->headers, Field);
                            str_split_trim(line, ":", &header->name, &header->value);
                        }
                    }

                stm_next(line, stm)

                stm_close(&stm);
                return TRUE;
            }
            else
            {
                http->error = ekIUNDEF;
                return FALSE;
            }
        }
        else
        {
            return FALSE;
        }
    }
    else
    {
        return TRUE;
    }
}

/*---------------------------------------------------------------------------*/

uint32_t http_response_status(const Http *http)
{
    if (i_response(cast(http, Http)) == TRUE)
        return http->rcode;
    else
        return UINT32_MAX;
}

/*---------------------------------------------------------------------------*/

const char_t *http_response_protocol(const Http *http)
{
    if (i_response(cast(http, Http)) == TRUE)
        return tc(http->rprotocol);
    else
        return "";
}

/*---------------------------------------------------------------------------*/

const char_t *http_response_message(const Http *http)
{
    if (i_response(cast(http, Http)) == TRUE)
        return tc(http->rmsg);
    else
        return "";
}

/*---------------------------------------------------------------------------*/

uint32_t http_response_size(const Http *http)
{
    if (i_response(cast(http, Http)) == TRUE)
        return arrst_size(http->headers, Field);
    else
        return 0;
}

/*---------------------------------------------------------------------------*/

const char_t *http_response_name(const Http *http, const uint32_t index)
{
    if (i_response(cast(http, Http)) == TRUE)
    {
        const Field *field = arrst_get(http->headers, index, Field);
        return tc(field->name);
    }
    else
    {
        return "";
    }
}

/*---------------------------------------------------------------------------*/

const char_t *http_response_value(const Http *http, const uint32_t index)
{
    if (i_response(cast(http, Http)) == TRUE)
    {
        const Field *field = arrst_get(http->headers, index, Field);
        return tc(field->value);
    }
    else
    {
        return "";
    }
}

/*---------------------------------------------------------------------------*/

const char_t *http_response_header(const Http *http, const char_t *name)
{
    if (i_response(cast(http, Http)) == TRUE)
    {
        const char_t *value = i_field(http->headers, name);
        if (value != NULL)
            return value;
        else
            return "";
    }
    else
    {
        return "";
    }
}

/*---------------------------------------------------------------------------*/

bool_t http_response_body(const Http *http, Stream *body, ierror_t *error)
{
    ierror_t lerror;
    cassert_no_null(http);
    oshttp_response_body(http->oshttp, body, &lerror);
    ptr_assign(error, lerror);
    return lerror == ekIOK ? TRUE : FALSE;
}

/*---------------------------------------------------------------------------*/

Stream *http_dget(const char_t *url, uint32_t *result, ierror_t *error)
{
    Url *uurl = url_parse(url);
    const char_t *sh = url_scheme(uurl);
    const char_t *host = url_host(uurl);
    uint16_t port = url_port(uurl);
    Http *http = NULL;
    Stream *stm = NULL;

    ptr_assign(error, ekIOK);

    if (str_equ_nocase(sh, "http") == TRUE)
        http = http_create(host, port);
    else if (str_equ_nocase(sh, "https") == TRUE)
        http = http_secure(host, port);

    if (http != NULL)
    {
        String *res = url_resource(uurl);
        if (http_get(http, tc(res), NULL, 0, error) == TRUE)
        {
            if (result != NULL)
                *result = http_response_status(http);

            stm = stm_memory(2048);
            if (http_response_body(http, stm, error) == FALSE)
                stm_close(&stm);
        }

        http_destroy(&http);
        str_destroy(&res);
    }
    else
    {
        ptr_assign(error, ekINOHOST);
    }

    url_destroy(&uurl);
    return stm;
}

/*---------------------------------------------------------------------------*/

bool_t http_exists(const char_t *url)
{
    bool_t exists = FALSE;
    Url *uurl = url_parse(url);
    const char_t *sh = url_scheme(uurl);
    const char_t *host = url_host(uurl);
    uint16_t port = url_port(uurl);
    Http *http = NULL;

    if (str_equ_nocase(sh, "http") == TRUE)
        http = http_create(host, port);
    else if (str_equ_nocase(sh, "https") == TRUE)
        http = http_secure(host, port);

    if (http != NULL)
    {
        String *res = url_resource(uurl);
        i_clear_response(http);
        oshttp_get(http->oshttp, tc(res), NULL, 0, FALSE, &http->error);
        if (http->error == ekIOK)
        {
            uint32_t result = http_response_status(http);
            if (result >= 200 && result <= 299)
                exists = TRUE;
        }

        http_destroy(&http);
        str_destroy(&res);
    }

    url_destroy(&uurl);
    return exists;
}