dylanweber

filterURLstring in C

Mar 10th, 2015
273
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4.  
  5. void filterURLstring(char *weburl, char **protocol, char **username, char **password, char **host, char **port, char **path);
  6.  
  7. int main() {
  8.     char *text = "http://mhdfffffjddj:abc123@192.168.0.2:8888/servlet/rece/";
  9.     char *protocol, *username, *password, *host, *port, *path;
  10.     filterURLstring(text, &protocol, &username, &password, &host, &port, &path);
  11.     printf("Protocol:\"%s\"\n", protocol);
  12.     printf("Username:\"%s\"\n", username);
  13.     printf("Password:\"%s\"\n", password);
  14.     printf("Host:\"%s\"\n", host);
  15.     printf("Port:\"%s\"\n", port);
  16.     printf("Path:\"%s\"\n", path);
  17.     free(protocol);
  18.     free(username);
  19.     free(password);
  20.     free(host);
  21.     free(port);
  22.     free(path);
  23.     return 0;
  24. }
  25.  
  26. void filterURLstring(char *weburl, char **protocol, char **username, char **password, char **host, char **port, char **path) {
  27.     char *postprotocol, *postuserinfo, *postusername, *login, *posthost, *posthostname, *hostloc, *postport;
  28.     int isPath;
  29.     if ((postprotocol = strstr(weburl, "://")) == NULL) {
  30.         printf("Input URL is invalid. Exiting...\n");
  31.         exit(1);
  32.     }
  33.     *protocol = malloc(sizeof(char) * (postprotocol - weburl + 1));
  34.     strncpy(*protocol, weburl, postprotocol - weburl);
  35.     postprotocol = (postprotocol + (*(postprotocol+3)=='/'?4:3));
  36.     if ((postuserinfo = strstr(postprotocol, "@")) != NULL) {
  37.         if ((postusername = strchr(postprotocol, ':')) != NULL) {
  38.             *password = malloc(sizeof(char) * (postuserinfo - postusername + 1));
  39.             strncpy(*password, postusername + 1, (postuserinfo - postusername - 1));
  40.         } else {
  41.             *password = NULL;
  42.         }
  43.         int userlen = (postusername?postusername:postuserinfo) - postprotocol;
  44.         *username = malloc(sizeof(char) *  userlen + 1);
  45.         strncpy(*username, postprotocol, userlen);
  46.     }
  47.     hostloc = (postuserinfo?postuserinfo + 1:postprotocol);
  48.     if ((posthost = strchr(hostloc, '/')) == NULL) {
  49.         posthost = hostloc + strlen(hostloc);
  50.         isPath = 1;
  51.     }
  52.     if ((posthostname = strchr(hostloc, ':')) == NULL) {
  53.         posthostname = posthost;
  54.     }
  55.     *host = malloc(sizeof(char) * (posthostname - hostloc + 1));
  56.     strncpy(*host, hostloc, (posthostname - hostloc));
  57.     if (posthostname != posthost) {
  58.         posthostname++;
  59.         *port = malloc(sizeof(char) * (posthost - posthostname + 1));
  60.         strncpy(*port, posthostname, (posthost - posthostname));
  61.     } else {
  62.         *port = malloc(sizeof(char) * 2 + 1);
  63.         strncpy(*port, "80", 2);
  64.     }
  65.     if (isPath) {
  66.         *path = malloc(sizeof(char) * strlen(posthost));
  67.         strncpy(*path, posthost, strlen(posthost));
  68.     } else {
  69.         *path = calloc(1, 1);
  70.     }
  71. }
RAW Paste Data