-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathurl.c
81 lines (68 loc) · 2.2 KB
/
url.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
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include "json/json.h"
#include "../src/url/minicrawler-url.h"
const char *encode(char *input) {
struct JSON_Value *val = JSON_Value_New_String(input);
const char *encoded = JSON_Encode(val, 0, 0);
JSON_Value_Free(val);
return encoded;
}
int main(int argc, char *argv[]) {
if (argc < 2) {
exit(1);
}
char *input = argv[1];
char *base = NULL;
if (argc > 2) {
base = argv[2];
}
mcrawler_url_url url, *base_url = NULL;
if (base) {
base_url = (mcrawler_url_url *)malloc(sizeof(mcrawler_url_url));
if (mcrawler_url_parse(base_url, base, NULL) == MCRAWLER_URL_FAILURE) {
printf("{\"input\": %s, \"base\": %s, \"failure\": true}", encode(input), encode(base));
mcrawler_url_free_url(base_url);
free(base_url);
exit(0);
}
} else {
base = "";
}
if (mcrawler_url_parse(&url, input, base_url) == MCRAWLER_URL_FAILURE) {
printf("{\"input\": %s, \"base\": %s, \"failure\": true}", encode(input), encode(base));
if (base_url) {
mcrawler_url_free_url(base_url);
free(base_url);
}
exit(0);
}
if (base_url) {
mcrawler_url_free_url(base_url);
free(base_url);
}
char *href = mcrawler_url_get_href(&url);
char *protocol = mcrawler_url_get_protocol(&url);
char *username = mcrawler_url_get_username(&url);
char *password = mcrawler_url_get_password(&url);
char *hostname = mcrawler_url_get_hostname(&url, NULL);
char *host = mcrawler_url_get_host(&url, NULL);
char *port = mcrawler_url_get_port(&url);
char *pathname = mcrawler_url_get_pathname(&url);
char *search = mcrawler_url_get_search(&url);
char *hash = mcrawler_url_get_hash(&url);
printf("{\"input\": %s, \"base\": %s, \"href\": %s, \"protocol\": %s, \"username\": %s, \"password\": %s, \"host\": %s, \"hostname\": %s, \"port\": %s, \"pathname\": %s, \"search\": %s, \"hash\": %s}", encode(input), encode(base), encode(href), encode(protocol), encode(username), encode(password), encode(host), encode(hostname), encode(port), encode(pathname), encode(search), encode(hash));
free(href);
free(protocol);
free(username);
free(password);
free(hostname);
free(host);
free(port);
free(pathname);
free(search);
free(hash);
mcrawler_url_free_url(&url);
exit(0);
}