summaryrefslogtreecommitdiffstats
path: root/tinyHTTP/src/thttp_url.c
blob: 4474d1ad14df520618e9457becc11436d4718ae1 (plain)
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
/*
* Copyright (C) 2010-2011 Mamadou Diop.
*
* Contact: Mamadou Diop <diopmamadou(at)doubango[dot]org>
*
* This file is part of Open Source Doubango Framework.
*
* DOUBANGO is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* DOUBANGO is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with DOUBANGO.
*
*/

/**@file thttp_url.c
 * @brief HTTP URL.
 *
 * @author Mamadou Diop <diopmamadou(at)doubango[dot]org>
 *

 */
#include "tinyhttp/thttp_url.h"

#include "tinyhttp/parsers/thttp_parser_url.h"

#include "tsk_debug.h"
#include "tsk_memory.h"
#include "tsk_string.h"

#include <string.h>

/**@defgroup thttp_url_group HTTP/HTTPS URL
*/

/**@ingroup thttp_url_group
* @param type The type of the url to create.
* Creates new HTTP/HTTPS url.
*/
thttp_url_t* thttp_url_create(thttp_url_type_t type)
{
    return tsk_object_new(thttp_url_def_t, type);
}

/**@ingroup thttp_url_group
*/
int thttp_url_serialize(const thttp_url_t *url, tsk_buffer_t *output)
{
    /* RFC 1738: "http://" hostport [ "/" hpath [ "?" search ]] */
    return tsk_buffer_append_2(output, "%s://%s:%u%s%s%s%s",

                               url->scheme,
                               url->host,
                               url->port,

                               url->hpath ? "/" : "",
                               url->hpath ? url->hpath : "",

                               url->search ? "?" : "",
                               url->search ? url->search : ""
                              );
}

/**@ingroup thttp_url_group
*/
char* thttp_url_tostring(const thttp_url_t *url)
{
    tsk_buffer_t *output = tsk_buffer_create_null();
    char* ret = 0;

    if(!thttp_url_serialize(url,  output)) {
        ret = tsk_strndup((const char*)output->data, output->size);
    }
    else {
        TSK_DEBUG_ERROR("Failed to serialize HTTP URL.");
    }

    TSK_OBJECT_SAFE_FREE(output);
    return ret;
}

/**@ingroup thttp_url_group
*/
thttp_url_t *thttp_url_clone(const thttp_url_t *url)
{
    thttp_url_t *newurl = 0;
    tsk_buffer_t *output = tsk_buffer_create_null();
    thttp_url_serialize(url, output);
    newurl = thttp_url_parse(output->data, output->size);
    TSK_OBJECT_SAFE_FREE(output);

    return newurl;
}

/**@ingroup thttp_url_group
*/
tsk_bool_t thttp_url_isvalid(const char* urlstring)
{
    thttp_url_t *url;
    if(!urlstring) {
        return tsk_false;
    }

    if((url = thttp_url_parse(urlstring, tsk_strlen(urlstring)))) {
        TSK_OBJECT_SAFE_FREE(url);
        return tsk_true;
    }
    else {
        return tsk_false;
    }
}






//========================================================
//	HTTP/HTTPS URL object definition
//

/**@ingroup thttp_url_group
*/
static tsk_object_t* thttp_url_ctor(tsk_object_t *self, va_list * app)
{
    thttp_url_t *url = self;
    if(url) {
        url->type = va_arg(*app, thttp_url_type_t);
        if(url->type == thttp_url_https) {
            url->port = 443;
        }
        else {
            url->port = 80;
        }
    }
    else {
        TSK_DEBUG_ERROR("Failed to create new SIP/SIPS/TEL.");
    }
    return self;
}

/**@ingroup thttp_url_group
*/
static tsk_object_t* thttp_url_dtor(tsk_object_t *self)
{
    thttp_url_t *url = self;
    if(url) {
        TSK_FREE(url->scheme);
        TSK_FREE(url->host);
        TSK_FREE(url->hpath);
        TSK_FREE(url->search);
    }
    else {
        TSK_DEBUG_ERROR("Null HTTP/HTTPS URL.");
    }

    return self;
}

static const tsk_object_def_t thttp_url_def_s = {
    sizeof(thttp_url_t),
    thttp_url_ctor,
    thttp_url_dtor,
    tsk_null
};
const tsk_object_def_t *thttp_url_def_t = &thttp_url_def_s;
OpenPOWER on IntegriCloud