pastebin - collaborative debugging tool
nrubsig.kpaste.net RSS

Difference between
modified post 544ee372 by Anonymous on Wed 24th Jan 2024 10:56
original post db6ed32 by Anonymous on Wed 24th Jan 2024 10:29
Download diff
Show old version | new version | both versions

    
11
/*
22
 * MIT License
33
 *
44
 * Copyright (c) 2024 Roland Mainz <roland.mainz@nrubsig.org>
55
 *
66
 * Permission is hereby granted, free of charge, to any person obtaining a copy
77
 * of this software and associated documentation files (the "Software"), to deal
88
 * in the Software without restriction, including without limitation the rights
99
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
1010
 * copies of the Software, and to permit persons to whom the Software is
1111
 * furnished to do so, subject to the following conditions:
1212
 *
1313
 * The above copyright notice and this permission notice shall be included in all
1414
 * copies or substantial portions of the Software.
1515
 *
1616
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
1717
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
1818
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
1919
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
2020
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
2121
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
2222
 * SOFTWARE.
2323
 */
2424
25+
/* urlparser1.c - simple URL parser */
2527
#include <stdlib.h>
2628
#include <stdbool.h>
2729
#include <string.h>
2830
#include <stdio.h>
2931
3032
/*
3133
 * Original extended regular expression:
3234
 *
3335
 * "^"
3436
 * "(.+?)"			 // scheme
3537
 * "://"  			 // '://'
3638
 * "("				 // login
3739
 * 	 "(?:"
3840
 * 		 "(.+?)"	 // user (optional)
3941
 * 		 "(?::(.+))?"	 // password (optional)
4042
 * 		 "@"
4143
 * 	 ")?"
4244
 * 	 "("			 // hostport
4345
 * 		 "(.+?)"	 // host
4446
 * 		 "(?::([[:digit:]]+))?" // port (optional)
4547
 * 	 ")"
4648
 * ")"
4749
 * "(?:/(.*?))?"  		 // path (optional)
4850
 * "$"
4951
 */
5052
53+
#define DBGNULLSTR(s) (((s)!=NULL)?(s):"<NULL>")
5156
typedef
5257
struct _url_parser_context {
5358
	char *in_url;
5459
5560
	char *scheme;
5661
	struct {
57-
		char *login;
62+
		char *username;
63+
		char *passwd;
5864
	} login;
5965
	struct {
6066
		char *hostname;
6167
		signed int port;
6268
	} hostport;
6369
	char *path;
6470
} url_parser_context;
6571
66-
url_parser_context *url_parser_create_context(const char *in_url)
72+
url_parser_context *url_parser_create_context(const char *in_url, unsigned int flags)
6773
{
6874
	url_parser_context *uctx;
6975
	char *s;
7076
	size_t in_url_len;
7177
	size_t context_len;
7278
7379
	if (!in_url)
7480
		return NULL;
7581
7682
	in_url_len = strlen(in_url);
7783
7884
	context_len = sizeof(url_parser_context) +
7985
		((in_url_len+1)*5);
8086
	uctx = malloc(context_len);
8187
	if (!uctx)
8288
		return NULL;
8389
8490
	s = (void *)(uctx+1);
85-
	uctx->in_url = s;	s+= in_url_len+1;
91+
	uctx->in_url = s;		s+= in_url_len+1;
8692
	(void)strcpy(uctx->in_url, in_url);
87-
	uctx->scheme = s;	s+= in_url_len+1;
93+
	uctx->scheme = s;		s+= in_url_len+1;
88-
	uctx->login.login = s;	s+= in_url_len+1;
94+
	uctx->login.username = s;	s+= in_url_len+1;
8995
	uctx->hostport.hostname = s;	s+= in_url_len+1;
90-
	uctx->path = s;	s+= in_url_len+1;
96+
	uctx->path = s;			s+= in_url_len+1;
97+
	uctx->hostport.port = -1;
9198
9299
	return uctx;
93100
}
94101
95-
int url_parser(url_parser_context *uctx)
102+
int url_parser_parse(url_parser_context *uctx)
96103
{
97104
	(void)fprintf(stderr, "## parser in_url='%s'\n", uctx->in_url);
98105
99106
	char *s;
100107
	const char *urlstr = uctx->in_url;
101108
	size_t slen;
102109
103110
	s = strstr(urlstr, "://");
104111
	if (!s) {
105112
		(void)fprintf(stderr, "url_parser: Not an URL\n");
106113
		return -1;
107114
	}
108115
109116
	slen = s-urlstr;
110117
	(void)memcpy(uctx->scheme, urlstr, slen);
111118
	uctx->scheme[slen] = '\0';
112119
	urlstr += slen + 3;
113120
114121
	(void)fprintf(stdout, "scheme='%s', rest='%s'\n", uctx->scheme, urlstr);
115122
116123
	s = strstr(urlstr, "@");
117124
	if (s) {
118125
		/* URL has user/password */
119126
		slen = s-urlstr;
120-
		(void)memcpy(uctx->login.login, urlstr, slen);
127+
		(void)memcpy(uctx->login.username, urlstr, slen);
121-
		uctx->login.login[slen] = '\0';
128+
		uctx->login.username[slen] = '\0';
122129
		urlstr += slen + 1;
123130
124-
		(void)fprintf(stdout, "login='%s', rest='%s'\n", uctx->login.login, urlstr);
131+
		s = strstr(uctx->login.username, ":");
132+
		if (s) {
125136
	}
126137
	else
127138
	{
128-
		uctx->login.login = NULL;
139+
			uctx->login.passwd = NULL;
129140
	}
130141
131-
	s = strstr(urlstr, "/");
142+
		/* catch password-only URLs */
132-
	if (s) {
143+
		if (uctx->login.username[0] == '\0')
133-
		/* URL has hostport */
144+
			uctx->login.username = NULL;
134-
		slen = s-urlstr;
145+
	}
135-
		(void)memcpy(uctx->hostport.hostname, urlstr, slen);
146+
	else
136-
		uctx->hostport.hostname[slen] = '\0';
147+
	{
137-
		urlstr += slen + 1;
148+
		uctx->login.username = NULL;
149+
	}
138150
139-
		(void)fprintf(stdout, "hostport='%s', rest='%s'\n", uctx->hostport.hostname, urlstr);
151+
	(void)fprintf(stdout, "login='%s', passwd='%s', rest='%s'\n",
152+
		DBGNULLSTR(uctx->login.username),
140155
156+
	s = strstr(urlstr, "/");
142157
		if (s) {
158+
		/* URL has hostport */
143166
			/* found port number */
144167
			uctx->hostport.port = atoi(s+1);
145168
			*s = '\0';
146169
		}
170+
	}
147171
		else
148172
		{
149-
			uctx->hostport.port = -1;
173+
		(void)strcpy(uctx->hostport.hostname, urlstr);
174+
		uctx->path = NULL;
150176
		}
151177
178+
	(void)fprintf(stdout, "hostport='%s', port=%d, rest='%s'\n",

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