summaryrefslogtreecommitdiff
path: root/src/bin
diff options
context:
space:
mode:
Diffstat (limited to 'src/bin')
-rw-r--r--src/bin/cuturl.c141
1 files changed, 141 insertions, 0 deletions
diff --git a/src/bin/cuturl.c b/src/bin/cuturl.c
new file mode 100644
index 0000000..945d640
--- /dev/null
+++ b/src/bin/cuturl.c
@@ -0,0 +1,141 @@
+#include <stdio.h>
+#include <string.h>
+/*
+ schemes are case sensitive but cononicals are lower case.
+ domain is case insensitive. return it lowercased.
+ port is optional and in decimal
+ path
+ scheme://username:password@domain:port/path?query_string#fragment_id
+ mailto:username@domain
+
+ optional stuff:
+ scheme, username, password, port, path, query_string, fragment_id
+*/
+
+/*
+#define DEFAULT_SCHEME "http"
+#define DEFAULT_USERNAME "anonymous"
+#define DEFAULT_PASSWORD "anonymous"
+#define DEFAULT_PORT "80"
+#define DEFAULT_QUERY_STRING ""
+#define DEFAULT_FRAGMENT_ID ""
+*/
+#define DEFAULT_SCHEME "DEFAULT"
+#define DEFAULT_USERNAME "DEFAULT"
+#define DEFAULT_PASSWORD "DEFAULT"
+#define DEFAULT_PORT "DEFAULT"
+#define DEFAULT_PATH "DEFAULT"
+#define DEFAULT_QUERY_STRING "DEFAULT"
+#define DEFAULT_FRAGMENT_ID "DEFAULT"
+
+int main() {
+ char line[1024];
+ char *scheme=0;
+ char *username=0;
+ char *password=0;
+ char *domain=0;
+ char *port=0;
+ char *path=0;
+ char *query_string=0;
+ char *fragment_id=0;
+ int i;
+ fgets(line,sizeof(line),stdin);
+ for(i=0;line[i] && line[i] != '\n' && line[i] != '\r';i++);
+ line[i]=0;
+
+ //split at first single / into line and path
+ for(i=0;line[i];i++) {
+ if(line[i] == '/' && line[i+1] == '/') {
+ i++;
+ continue;
+ }
+ if(line[i] == '/') {
+ line[i]=0;
+ path=line+i+1;
+ break;
+ }
+ }
+
+ if(path) {
+ if(strchr(path,'?')) {
+ query_string=strchr(path,'?');
+ *query_string=0;
+ query_string++;
+ }
+ }
+
+ if(query_string) {
+ if(strchr(query_string,'#')) {
+ fragment_id=strchr(query_string,'#');
+ *fragment_id=0;
+ fragment_id++;
+ }
+ }
+
+ if(strstr(line,"://")) {
+ scheme=line;
+ domain=strstr(line,"://");
+ *domain=0;
+ domain+=3;
+ } else {
+ domain=line;
+ }
+
+ if(domain) {
+ if(strchr(domain,'@')) {
+ username=domain;
+ domain=strchr(domain,'@');
+ *domain=0;
+ domain++;
+ }
+ }
+
+ if(username) {
+ if(strchr(username,':')) {
+ password=strchr(username,':');
+ *password=0;
+ password++;
+ }
+ }
+
+ if(domain) {
+ if(strchr(domain,']')) {
+ if(strchr(strchr(domain,']'),':')) {
+ port=strchr(strchr(domain,']'),':');
+ *port=0;
+ port++;
+ }
+ } else {
+ if(strchr(domain,':')) {
+ port=strchr(domain,':');
+ *port=0;
+ port++;
+ }
+ }
+ }
+
+ if(domain) {
+ if(strchr(domain,'?')) {
+ query_string=strchr(domain,'?');
+ *query_string=0;
+ query_string++;
+ }
+ }
+
+ if(!scheme && username) {
+ scheme=username;
+ username=password;
+ password=0;
+ }
+
+// printf("scheme://username:password@domain:port/path?query_string#fragment_id\n\n");
+ printf("scheme: %s\n",scheme);
+ printf("username: %s\n",username);
+ printf("password: %s\n",password);
+ printf("domain: %s\n",domain);
+ printf("port: %s\n",port);
+ printf("path: %s\n",path);
+ printf("query_string: %s\n",query_string);
+ printf("fragment_id: %s\n",fragment_id);
+ return 0;
+}