-
Notifications
You must be signed in to change notification settings - Fork 2
/
urlparse.c
87 lines (79 loc) · 1.98 KB
/
urlparse.c
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
/*
* urlparse.c
*
* Created by Yigit Colakoglu on 07/06/2021.
* Copyright yigit@yigitcolakoglu.com. 2021. All rights reserved.
*/
#include "urlparse.h"
#include "linkedlist.h"
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
URL *parseurl(char *url) {
URL *urlp = urlalloc();
urlp->params = NULL;
urlp->nparams = 0;
short stage = 0; /* var to keep track of where we are in url */
int counter = 0;
while (*url != '\0' && *url != '\n') {
switch (*url++) {
case ':':
counter++;
if (stage == 0) {
urlp->https = *(url - 2) == 's';
if (*(url + 1) == '\0' || *url == '\0' || *url == '\n') /* weird stuff would happen with strings like "http:" */
return NULL;
url += 2; /* Skip the // after the :*/
stage = 1;
counter+=3;
}
break;
case '?':
if (stage == 1) {
urlp->base =
(char *)malloc(counter); /* +1 for the '\0' in the end */
strncpy(urlp->base, url - counter, counter - 1);
stage = 2;
counter = 1;
} else {
return NULL;
}
break;
case '=':
if (stage == 2) {
char *foo;
foo = (char *)malloc(counter);
strncpy(foo, url - counter, counter-1);
counter = 1;
if (urlp->params == NULL){
urlp->params = linkedlistalloc();
urlp->params->data = foo;
}else
urlp->params = linkedlistadd(urlp->params, foo);
urlp->nparams++;
while(*url != '&' && *url != '\0' && *url != '\n')
url++;
url++;
}
break;
default:
counter++;
break;
}
}
switch(stage){
case 0:
return NULL;
break;
case 1:
urlp->base = (char *)malloc(counter); /* +1 for the '\0' in the end */
strncpy(urlp->base, url - (counter-1), counter - 1);
break;
case 2:
break;
default:
return NULL;
}
return urlp;
}
URL *urlalloc(void) { return (URL *)malloc(sizeof(URL)); }