-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsc_uri.h
113 lines (103 loc) · 3.48 KB
/
sc_uri.h
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
/*
* BSD-3-Clause
*
* Copyright 2021 Ozan Tezcan
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
* 3. Neither the name of the copyright holder nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
* OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
* OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
#ifndef SC_URI_H
#define SC_URI_H
#define SC_URI_VERSION "2.0.0"
#ifdef SC_HAVE_CONFIG_H
#include "config.h"
#else
#define sc_uri_malloc malloc
#define sc_uri_free free
#endif
#include <stdbool.h>
#include <stdlib.h>
/**
* Based on RFC 3986
*
* The following is a example URIs and their component parts:
*
* foo://user:[email protected]:8042/over/there?name=ferret#nose
* \_/ \____________________________/\_________/ \_________/ \__/
* | | | | |
* scheme authority path query fragment
*
*
* user:[email protected]:8042
* \__________/ \_________/ \__/
* | | |
* userinfo host port
* --------------------------------------------------------------------
*
*/
struct sc_uri {
const char *str; // Full string
const char *scheme;
const char *host;
const char *userinfo;
const char *port;
const char *path;
const char *query;
const char *fragment;
char buf[];
};
/**
* Parse uri.
*
* Internally, it does a single allocation but each part is also represented as
* NULL ended string.
*
* E.g :
*
* struct sc_uri* uri;
*
* struct sc_uri* uri;
* uri =
* sc_uri_create("http://user:[email protected]:8042/over/there?name=jane#doe");
*
* printf("%s \n", uri->str); // prints full string.
* printf("%s \n", uri->scheme); // prints "http"
* printf("%s \n", uri->host); // prints "any.com"
* printf("%s \n", uri->userinfo); // prints "user:pass"
* printf("%s \n", uri->port); // prints "8042"
* printf("%s \n", uri->path); // prints "/over/there"
* printf("%s \n", uri->query); // prints "name=jane"
* printf("%s \n", uri->fragment); // prints "doe"
*
*
* @param str uri string
* @return uri, NULL on error
*/
struct sc_uri *sc_uri_create(const char *str);
/**
* @param uri uri
*/
void sc_uri_destroy(struct sc_uri **uri);
#endif