pastebin - collaborative debugging tool
nrubsig.kpaste.net RSS

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

    
1+
/* urlparser1.c */
12
/*
23
 * MIT License
34
 *
45
 * Copyright (c) 2024 Roland Mainz <roland.mainz@nrubsig.org>
56
 *
67
 * Permission is hereby granted, free of charge, to any person obtaining a copy
78
 * of this software and associated documentation files (the "Software"), to deal
89
 * in the Software without restriction, including without limitation the rights
910
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
1011
 * copies of the Software, and to permit persons to whom the Software is
1112
 * furnished to do so, subject to the following conditions:
1213
 *
1314
 * The above copyright notice and this permission notice shall be included in all
1415
 * copies or substantial portions of the Software.
1516
 *
1617
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
1718
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
1819
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
1920
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
2021
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
2122
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
2223
 * SOFTWARE.
2324
 */
2425
2526
/* urlparser1.c - simple URL parser */
2627
2728
#include <stdlib.h>
2829
#include <stdbool.h>
2930
#include <string.h>
3031
#include <stdio.h>
3132
33+
#include "urlparser1.h"
3235
/*
3336
 * Original extended regular expression:
3437
 *
3538
 * "^"
3639
 * "(.+?)"			 // scheme
3740
 * "://"  			 // '://'
3841
 * "("				 // login
3942
 * 	 "(?:"
4043
 * 		 "(.+?)"	 // user (optional)
4144
 * 		 "(?::(.+))?"	 // password (optional)
4245
 * 		 "@"
4346
 * 	 ")?"
4447
 * 	 "("			 // hostport
4548
 * 		 "(.+?)"	 // host
4649
 * 		 "(?::([[:digit:]]+))?" // port (optional)
4750
 * 	 ")"
4851
 * ")"
4952
 * "(?:/(.*?))?"  		 // path (optional)
5053
 * "$"
5154
 */
5255
5356
#define DBGNULLSTR(s) (((s)!=NULL)?(s):"<NULL>")
57+
#if 1
5462
63+
url_parser_context *url_parser_create_context(const char *in_url, unsigned int flags)
5569
56-
typedef
70+
	if (!in_url)
57-
struct _url_parser_context {
71+
		return NULL;
5972
60-
	char *scheme;
73+
	in_url_len = strlen(in_url);
61-
	struct {
74+
62-
		char *username;
75+
	context_len = sizeof(url_parser_context) +
63-
		char *passwd;
76+
		((in_url_len+1)*5);
64-
	} login;
77+
	uctx = malloc(context_len);
65-
	struct {
78+
	if (!uctx)
66-
		char *hostname;
79+
		return NULL;
7180
72-
url_parser_context *url_parser_create_context(const char *in_url, unsigned int flags)
81+
	s = (void *)(uctx+1);
73-
{
82+
	uctx->in_url = s;		s+= in_url_len+1;
74-
	url_parser_context *uctx;
83+
	(void)strcpy(uctx->in_url, in_url);
75-
	char *s;
84+
	uctx->scheme = s;		s+= in_url_len+1;
76-
	size_t in_url_len;
85+
	uctx->login.username = s;	s+= in_url_len+1;
77-
	size_t context_len;
86+
	uctx->hostport.hostname = s;	s+= in_url_len+1;
87+
	uctx->path = s;			s+= in_url_len+1;
7889
79-
	if (!in_url)
90+
	return uctx;
80-
		return NULL;
91+
}
8192
82-
	in_url_len = strlen(in_url);
93+
int url_parser_parse(url_parser_context *uctx)
94+
{
8396
84-
	context_len = sizeof(url_parser_context) +
97+
	char *s;
85-
		((in_url_len+1)*5);
98+
	const char *urlstr = uctx->in_url;
86-
	uctx = malloc(context_len);
99+
	size_t slen;
89100
90-
	s = (void *)(uctx+1);
101+
	s = strstr(urlstr, "://");
91-
	uctx->in_url = s;		s+= in_url_len+1;
102+
	if (!s) {
92-
	(void)strcpy(uctx->in_url, in_url);
103+
		D((void)fprintf(stderr, "url_parser: Not an URL\n"));
93-
	uctx->scheme = s;		s+= in_url_len+1;
104+
		return -1;
105+
	}
98106
99-
	return uctx;
107+
	slen = s-urlstr;
100-
}
108+
	(void)memcpy(uctx->scheme, urlstr, slen*sizeof(char));
109+
	uctx->scheme[slen] = '\0';
101111
102-
int url_parser_parse(url_parser_context *uctx)
112+
	D((void)fprintf(stdout, "scheme='%s', rest='%s'\n", uctx->scheme, urlstr));
105113
106-
	char *s;
114+
	s = strstr(urlstr, "@");
107-
	const char *urlstr = uctx->in_url;
115+
	if (s) {
108-
	size_t slen;
116+
		/* URL has user/password */
117+
		slen = s-urlstr;
109121
110-
	s = strstr(urlstr, "://");
122+
		s = strstr(uctx->login.username, ":");
111-
	if (!s) {
123+
		if (s) {
112-
		(void)fprintf(stderr, "url_parser: Not an URL\n");
124+
			/* found passwd */
113-
		return -1;
125+
			uctx->login.passwd = s+1;
126+
			*s = '\0';
114127
	}
128+
		else
115132
116-
	slen = s-urlstr;
133+
		/* catch password-only URLs */
117-
	(void)memcpy(uctx->scheme, urlstr, slen);
134+
		if (uctx->login.username[0] == '\0')
118-
	uctx->scheme[slen] = '\0';
135+
			uctx->login.username = NULL;
119-
	urlstr += slen + 3;
136+
	}
137+
	else
120142
121-
	(void)fprintf(stdout, "scheme='%s', rest='%s'\n", uctx->scheme, urlstr);
143+
	D((void)fprintf(stdout, "login='%s', passwd='%s', rest='%s'\n",
144+
		DBGNULLSTR(uctx->login.username),
122147
123-
	s = strstr(urlstr, "@");
148+
	s = strstr(urlstr, "/");
124149
	if (s) {
125-
		/* URL has user/password */
150+
		/* URL has hostport */
126151
		slen = s-urlstr;
127-
		(void)memcpy(uctx->login.username, urlstr, slen);
152+
		(void)memcpy(uctx->hostport.hostname, urlstr, slen*sizeof(char));
128-
		uctx->login.username[slen] = '\0';
153+
		uctx->hostport.hostname[slen] = '\0';
129-
		urlstr += slen + 1;
154+
		urlstr += slen;
130155
131-
		s = strstr(uctx->login.username, ":");
156+
		s = strstr(uctx->hostport.hostname, ":");
132157
		if (s) {
133-
			/* found passwd */
158+
			/* found port number */
134-
			uctx->login.passwd = s+1;
159+
			uctx->hostport.port = atoi(s+1);
135160
			*s = '\0';
136161
		}
162+
	}
137163
		else
138164
		{
139-
			uctx->login.passwd = NULL;
165+
		(void)strcpy(uctx->hostport.hostname, urlstr);
166+
		uctx->path = NULL;
140168
		}
141169
142-
		/* catch password-only URLs */
170+
	D((void)fprintf(stdout, "hostport='%s', port=%d, rest='%s'\n",
143-
		if (uctx->login.username[0] == '\0')
171+
		DBGNULLSTR(uctx->hostport.hostname),
144-
			uctx->login.username = NULL;
172+
		uctx->hostport.port,
173+
		DBGNULLSTR(urlstr)));
145177
	}
178+

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