dylanweber

createURLdata & freeURLdata example

Mar 11th, 2015
49
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. typedef struct URLdata {
  6.     char *protocol;
  7.     char *username;
  8.     char *password;
  9.     char *host;
  10.     char *port;
  11.     char *path;
  12. } URLdata;
  13.  
  14. URLdata *createURLdata(char *weburl);
  15. void freeURLdata(URLdata *freeURL);
  16.  
  17. int main() {
  18.     char *text = "http://mhdfffffjddj:abc123@192.168.0.2:8888/servlet/rece/";
  19.     URLdata *inputURL = createURLdata(text);
  20.     printf("Protocol:\"%s\"\n", inputURL->protocol);
  21.     printf("Username:\"%s\"\n", inputURL->username);
  22.     printf("Password:\"%s\"\n", inputURL->password);
  23.     printf("Host:\"%s\"\n", inputURL->host);
  24.     printf("Port:\"%s\"\n", inputURL->port);
  25.     printf("Path:\"%s\"\n", inputURL->path);
  26.     freeURLdata(inputURL);
  27.     return 0;
  28. }
  29.  
  30. URLdata *createURLdata(char *weburl) {
  31.     char *postprotocol, *postuserinfo, *postusername, *login, *posthost, *posthostname, *hostloc, *postport;
  32.     int isPath, userlen;
  33.    
  34.     URLdata *returnURL = malloc(sizeof(URLdata));
  35.     returnURL->protocol = NULL;
  36.     returnURL->username = NULL;
  37.     returnURL->password = NULL;
  38.     returnURL->host = NULL;
  39.     returnURL->port = NULL;
  40.     returnURL->path = NULL;
  41.    
  42.     if ((postprotocol = strstr(weburl, "://")) == NULL) {
  43.         return returnURL;
  44.     }
  45.     returnURL->protocol = malloc(sizeof(char) * (postprotocol - weburl + 1));
  46.     strncpy(returnURL->protocol, weburl, postprotocol - weburl);
  47.     postprotocol = (postprotocol + (*(postprotocol+3)=='/'?4:3));
  48.     if ((postuserinfo = strstr(postprotocol, "@")) != NULL) {
  49.         if ((postusername = strchr(postprotocol, ':')) != NULL) {
  50.             returnURL->password = malloc(sizeof(char) * (postuserinfo - postusername + 1));
  51.             strncpy(returnURL->password, postusername + 1, (postuserinfo - postusername - 1));
  52.         }
  53.         userlen = (postusername?postusername:postuserinfo) - postprotocol;
  54.         returnURL->username = malloc(sizeof(char) *  userlen + 1);
  55.         strncpy(returnURL->username, postprotocol, userlen);
  56.     }
  57.     hostloc = (postuserinfo?postuserinfo + 1:postprotocol);
  58.     if ((posthost = strchr(hostloc, '/')) == NULL) {
  59.         posthost = hostloc + strlen(hostloc);
  60.         isPath = 1;
  61.     }
  62.     if ((posthostname = strchr(hostloc, ':')) == NULL) {
  63.         posthostname = posthost;
  64.     }
  65.     returnURL->host = malloc(sizeof(char) * (posthostname - hostloc + 1));
  66.     strncpy(returnURL->host, hostloc, (posthostname - hostloc));
  67.     if (posthostname != posthost) {
  68.         posthostname++;
  69.         returnURL->port = malloc(sizeof(char) * (posthost - posthostname + 1));
  70.         strncpy(returnURL->port, posthostname, (posthost - posthostname));
  71.     } else {
  72.         returnURL->port = malloc(sizeof(char) * 2 + 1);
  73.         strncpy(returnURL->port, "80", 2);
  74.     }
  75.     if (isPath) {
  76.         returnURL->path = malloc(sizeof(char) * strlen(posthost) + 1);
  77.         strncpy(returnURL->path, posthost, strlen(posthost));
  78.     } else {
  79.         returnURL->path = calloc(1, 1);
  80.     }
  81.     return returnURL;
  82. }
  83.  
  84. void freeURLdata(URLdata *freeURL) {
  85.     free(freeURL->protocol);
  86.     free(freeURL->username);
  87.     free(freeURL->password);
  88.     free(freeURL->host);
  89.     free(freeURL->port);
  90.     free(freeURL->path);
  91.     free(freeURL);
  92. }
RAW Paste Data