Add examples
This commit is contained in:
@@ -1,2 +1,6 @@
|
|||||||
test
|
test
|
||||||
test.exe
|
test.exe
|
||||||
|
reference.out
|
||||||
|
reference.exe
|
||||||
|
parse.out
|
||||||
|
parse.exe
|
||||||
|
|||||||
@@ -0,0 +1,2 @@
|
|||||||
|
gcc url.c examples/reference.c -o reference.exe -Wall -Wextra -ggdb
|
||||||
|
gcc url.c examples/parse.c -o parse.exe -Wall -Wextra -ggdb
|
||||||
@@ -0,0 +1,2 @@
|
|||||||
|
gcc url.c examples/reference.c -o reference.out -Wall -Wextra -ggdb
|
||||||
|
gcc url.c examples/parse.c -o parse.out -Wall -Wextra -ggdb
|
||||||
@@ -0,0 +1,62 @@
|
|||||||
|
#include <stdio.h>
|
||||||
|
#include <string.h>
|
||||||
|
#include "../url.h"
|
||||||
|
|
||||||
|
int main(void)
|
||||||
|
{
|
||||||
|
char src[] = "http://website.com/users/000?filter=no#title";
|
||||||
|
|
||||||
|
URL parsed;
|
||||||
|
int ret = url_parse(src, strlen(src), NULL, 1, &parsed);
|
||||||
|
if (ret < 0) {
|
||||||
|
printf("Couldn't parse base URL\n");
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
// http
|
||||||
|
printf("scheme=%.*s\n",
|
||||||
|
parsed.scheme.len,
|
||||||
|
parsed.scheme.ptr);
|
||||||
|
|
||||||
|
// (empty)
|
||||||
|
if (parsed.username.len == 0)
|
||||||
|
printf("username=(empty)\n");
|
||||||
|
else
|
||||||
|
printf("username=%.*s\n",
|
||||||
|
parsed.username.len,
|
||||||
|
parsed.username.ptr);
|
||||||
|
|
||||||
|
// (password)
|
||||||
|
if (parsed.password.len == 0)
|
||||||
|
printf("password=(empty)\n");
|
||||||
|
else
|
||||||
|
printf("password=%.*s\n",
|
||||||
|
parsed.password.len,
|
||||||
|
parsed.password.ptr);
|
||||||
|
|
||||||
|
if (parsed.host_type == URL_HOST_EMPTY)
|
||||||
|
printf("host=(empty)\n");
|
||||||
|
else
|
||||||
|
printf("host=%.*s\n",
|
||||||
|
parsed.host_text.len,
|
||||||
|
parsed.host_text.ptr);
|
||||||
|
|
||||||
|
if (parsed.no_port)
|
||||||
|
printf("port=(empty)\n");
|
||||||
|
else
|
||||||
|
printf("port=%d\n", parsed.port);
|
||||||
|
|
||||||
|
printf("path=%.*s\n",
|
||||||
|
parsed.path.len,
|
||||||
|
parsed.path.ptr);
|
||||||
|
|
||||||
|
printf("query=%.*s\n",
|
||||||
|
parsed.query.len,
|
||||||
|
parsed.query.ptr);
|
||||||
|
|
||||||
|
printf("fragment=%.*s\n",
|
||||||
|
parsed.fragment.len,
|
||||||
|
parsed.fragment.ptr);
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
@@ -0,0 +1,27 @@
|
|||||||
|
#include <stdio.h>
|
||||||
|
#include <string.h>
|
||||||
|
#include "../url.h"
|
||||||
|
|
||||||
|
int main(void)
|
||||||
|
{
|
||||||
|
char base[] = "http://website.com/users/000";
|
||||||
|
char ref[] = "../images/cat.png";
|
||||||
|
|
||||||
|
URL parsed_base;
|
||||||
|
int ret = url_parse(base, strlen(base), NULL, 1, &parsed_base);
|
||||||
|
if (ret < 0) {
|
||||||
|
printf("Couldn't parse base URL\n");
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
char dst[1<<10];
|
||||||
|
ret = url_resolve_reference(ref, strlen(ref), NULL, &parsed_base, 1, dst, (int) sizeof(dst));
|
||||||
|
if (ret < 0) {
|
||||||
|
printf("Couldn't resolve reference\n");
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
// http://website.com/images/cat.png
|
||||||
|
printf("%.*s\n", ret, dst);
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user