pastebin - collaborative debugging tool
nrubsig.kpaste.net RSS

Difference between
modified post d7e by Anonymous on Wed 24th Jan 2024 16:00
original post 14c22a62 by Anonymous on Wed 24th Jan 2024 15:58
Download diff
Show old version | new version | both versions

    
11
/* urlparser1.c */
22
/*
33
 * MIT License
44
 *
55
 * Copyright (c) 2024 Roland Mainz <roland.mainz@nrubsig.org>
66
 *
77
 * Permission is hereby granted, free of charge, to any person obtaining a copy
88
 * of this software and associated documentation files (the "Software"), to deal
99
 * in the Software without restriction, including without limitation the rights
1010
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
1111
 * copies of the Software, and to permit persons to whom the Software is
1212
 * furnished to do so, subject to the following conditions:
1313
 *
1414
 * The above copyright notice and this permission notice shall be included in all
1515
 * copies or substantial portions of the Software.
1616
 *
1717
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
1818
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
1919
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
2020
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
2121
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
2222
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
2323
 * SOFTWARE.
2424
 */
2525
2626
/* urlparser1.c - simple URL parser */
2727
2828
#include <stdlib.h>
2929
#include <stdbool.h>
3030
#include <string.h>
3131
#include <stdio.h>
3232
3333
#include "urlparser1.h"
3434
3535
/*
3636
 * Original extended regular expression:
3737
 *
3838
 * "^"
3939
 * "(.+?)"			 // scheme
4040
 * "://"  			 // '://'
4141
 * "("				 // login
4242
 * 	 "(?:"
4343
 * 		 "(.+?)"	 // user (optional)
4444
 * 		 "(?::(.+))?"	 // password (optional)
4545
 * 		 "@"
4646
 * 	 ")?"
4747
 * 	 "("			 // hostport
4848
 * 		 "(.+?)"	 // host
4949
 * 		 "(?::([[:digit:]]+))?" // port (optional)
5050
 * 	 ")"
5151
 * ")"
5252
 * "(?:/(.*?))?"  		 // path (optional)
5353
 * "$"
5454
 */
5555
5656
#define DBGNULLSTR(s) (((s)!=NULL)?(s):"<NULL>")
5757
#if 1
5858
#define D(x) x
5959
#else
6060
#define D(x)
6161
#endif
6262
6363
url_parser_context *url_parser_create_context(const char *in_url, unsigned int flags)
6464
{
6565
	url_parser_context *uctx;
6666
	char *s;
6767
	size_t in_url_len;
6868
	size_t context_len;
6969
7070
	if (!in_url)
7171
		return NULL;
7272
7373
	in_url_len = strlen(in_url);
7474
7575
	context_len = sizeof(url_parser_context) +
7676
		((in_url_len+1)*5);
7777
	uctx = malloc(context_len);
7878
	if (!uctx)
7979
		return NULL;
8080
8181
	s = (void *)(uctx+1);
8282
	uctx->in_url = s;		s+= in_url_len+1;
8383
	(void)strcpy(uctx->in_url, in_url);
8484
	uctx->scheme = s;		s+= in_url_len+1;
8585
	uctx->login.username = s;	s+= in_url_len+1;
8686
	uctx->hostport.hostname = s;	s+= in_url_len+1;
8787
	uctx->path = s;			s+= in_url_len+1;
8888
	uctx->hostport.port = -1;
8989
9090
	return uctx;
9191
}
9292
9393
int url_parser_parse(url_parser_context *uctx)
9494
{
9595
	D((void)fprintf(stderr, "## parser in_url='%s'\n", uctx->in_url));
9696
9797
	char *s;
9898
	const char *urlstr = uctx->in_url;
9999
	size_t slen;
100100
101101
	s = strstr(urlstr, "://");
102102
	if (!s) {
103103
		D((void)fprintf(stderr, "url_parser: Not an URL\n"));
104104
		return -1;
105105
	}
106106
107107
	slen = s-urlstr;
108108
	(void)memcpy(uctx->scheme, urlstr, slen*sizeof(char));
109109
	uctx->scheme[slen] = '\0';
110110
	urlstr += slen + 3;
111111
112112
	D((void)fprintf(stdout, "scheme='%s', rest='%s'\n", uctx->scheme, urlstr));
113113
114114
	s = strstr(urlstr, "@");
115115
	if (s) {
116116
		/* URL has user/password */
117117
		slen = s-urlstr;
118118
		(void)memcpy(uctx->login.username, urlstr, slen*sizeof(char));
119119
		uctx->login.username[slen] = '\0';
120120
		urlstr += slen + 1;
121121
122122
		s = strstr(uctx->login.username, ":");
123123
		if (s) {
124124
			/* found passwd */
125125
			uctx->login.passwd = s+1;
126126
			*s = '\0';
127127
		}
128128
		else
129129
		{
130130
			uctx->login.passwd = NULL;
131131
		}
132132
133133
		/* catch password-only URLs */
134134
		if (uctx->login.username[0] == '\0')
135135
			uctx->login.username = NULL;
136136
	}
137137
	else
138138
	{
139139
		uctx->login.username = NULL;
140140
		uctx->login.passwd = NULL;
141141
	}
142142
143143
	D((void)fprintf(stdout, "login='%s', passwd='%s', rest='%s'\n",
144144
		DBGNULLSTR(uctx->login.username),
145145
		DBGNULLSTR(uctx->login.passwd),
146146
		DBGNULLSTR(urlstr)));
147147
148148
	s = strstr(urlstr, "/");
149149
	if (s) {
150150
		/* URL has hostport */
151151
		slen = s-urlstr;
152152
		(void)memcpy(uctx->hostport.hostname, urlstr, slen*sizeof(char));
153153
		uctx->hostport.hostname[slen] = '\0';
154154
		urlstr += slen;
155155
156156
		s = strstr(uctx->hostport.hostname, ":");
157157
		if (s) {
158158
			/* found port number */
159159
			uctx->hostport.port = atoi(s+1);
160160
			*s = '\0';
161161
		}
162162
	}
163163
	else
164164
	{
165165
		(void)strcpy(uctx->hostport.hostname, urlstr);
166166
		uctx->path = NULL;
167167
		urlstr = NULL;
168168
	}
169169
170170
	D((void)fprintf(stdout, "hostport='%s', port=%d, rest='%s'\n",
171171
		DBGNULLSTR(uctx->hostport.hostname),
172172
		uctx->hostport.port,
173173
		DBGNULLSTR(urlstr)));
174174
175175
	if (!urlstr) {
176176
		return 0;
177177
	}
178178
179179
	(void)strcpy(uctx->path, urlstr);
180180
	D((void)fprintf(stdout, "path='%s'\n", uctx->path));
181181
182182
	return 0;
183183
}
184184
185185
void url_parser_free_context(url_parser_context *c)
186186
{
187187
	free(c);
188188
}
189189
190190
void test_url_parser(const char *instr)
191191
{
192192
	url_parser_context *c;
193193
194194
	c = url_parser_create_context(instr, 0);
195195
196196
	(void)url_parser_parse(c);
197197
198198
	(void)fputc('\n', stdout);
199199
200200
	url_parser_free_context(c);
201201
}
202202
203203
int main(int ac, char *av[])
204204
{
205205
	(void)puts("#start");
206206
207207
	(void)setvbuf(stdout, NULL, _IONBF, 0);
208208
	(void)setvbuf(stderr, NULL, _IONBF, 0);
209209
210210
	(void)test_url_parser("foo://hostbar/baz");
211211
	(void)test_url_parser("foo://myuser@hostbar/baz");
212212
	(void)test_url_parser("foo://myuser:mypasswd@hostbar/baz");
213213
	(void)test_url_parser("foo://myuser:mypasswd@hostbar:666/baz");
214214
	(void)test_url_parser("foo://:mypasswd2@hostbar2:667/baf");
215215
	(void)test_url_parser("foo://hostbar");
216216
	(void)test_url_parser("foo://hostbar:93");
217217
	(void)test_url_parser("foo://");
218218
	(void)test_url_parser("typo:/hostbar");
219219
	(void)test_url_parser("wrong");
220220
221221
	(void)puts("#done");
222222
223223
	return EXIT_SUCCESS;
224224
}
225225
/* urlparser1.h */
226-
#ifndef __URLPARSER1_H__
226+
/*
227-
#define __URLPARSER1_H__
227+
 * MIT License
228-
228+
 *
229-
#include <stdlib.h>
229+
 * Copyright (c) 2024 Roland Mainz <roland.mainz@nrubsig.org>
230-
230+
 *
231-
typedef
231+
 * Permission is hereby granted, free of charge, to any person obtaining a copy
232-
struct _url_parser_context {
232+
 * of this software and associated documentation files (the "Software"), to deal
233-
	char *in_url;
233+
 * in the Software without restriction, including without limitation the rights
234-
234+
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
235-
	char *scheme;
235+
 * copies of the Software, and to permit persons to whom the Software is
236-
	struct {
236+
 * furnished to do so, subject to the following conditions:
237-
		char *username;
237+
 *
238-
		char *passwd;
238+
 * The above copyright notice and this permission notice shall be included in all
239-
	} login;
239+
 * copies or substantial portions of the Software.
240-
	struct {
240+
 *
241-
		char *hostname;
241+
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
242-
		signed int port;
242+
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
243-
	} hostport;
243+
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
244-
	char *path;
244+
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
245-
} url_parser_context;
245+
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
246+
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
246249
247-
/* Prototypes */
250+
/* urlparser1.h - header for simple URL parser */
251251
252-
#endif /* !__URLPARSER1_H__ */
252+
#ifndef __URLPARSER1_H__
253+
#define __URLPARSER1_H__
253254
254-
255+
#include <stdlib.h>
255256
256-
257+
typedef
257-
258+
struct _url_parser_context {
258-
259+
	char *in_url;
259260
260-
261+
	char *scheme;
261-
262+
	struct {
262-
263+
		char *username;
263-
264+
		char *passwd;
264-
265+
	} login;
265-
266+
	struct {
266-
267+
		char *hostname;
267-
268+
		signed int port;
268-
269+
	} hostport;
269-
270+
	char *path;
270-
271+
} url_parser_context;
271272
272-
273+
/* Prototypes */
273-
274+
url_parser_context *url_parser_create_context(const char *in_url, unsigned int flags);
274-
275+
int url_parser_parse(url_parser_context *uctx);
275-
276+
void url_parser_free_context(url_parser_context *c);
276277
277-
278+
#endif /* !__URLPARSER1_H__ */
278279

Submit a correction or amendment below (click here to make a fresh posting)
After submitting an amendment, you'll be able to view the differences between the old and new posts easily.

Syntax highlighting:

To highlight particular lines, prefix each line with {%HIGHLIGHT}




All content is user-submitted.
The administrators of this site (kpaste.net) are not responsible for their content.
Abuse reports should be emailed to us at