Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- typedef struct URLdata {
- char *protocol;
- char *username;
- char *password;
- char *host;
- char *port;
- char *path;
- } URLdata;
- URLdata *createURLdata(char *weburl);
- void freeURLdata(URLdata *freeURL);
- int main() {
- char *text = "http://mhdfffffjddj:abc123@192.168.0.2:8888/servlet/rece/";
- URLdata *inputURL = createURLdata(text);
- printf("Protocol:\"%s\"\n", inputURL->protocol);
- printf("Username:\"%s\"\n", inputURL->username);
- printf("Password:\"%s\"\n", inputURL->password);
- printf("Host:\"%s\"\n", inputURL->host);
- printf("Port:\"%s\"\n", inputURL->port);
- printf("Path:\"%s\"\n", inputURL->path);
- freeURLdata(inputURL);
- return 0;
- }
- URLdata *createURLdata(char *weburl) {
- char *postprotocol, *postuserinfo, *postusername, *login, *posthost, *posthostname, *hostloc, *postport;
- int isPath, userlen;
- URLdata *returnURL = malloc(sizeof(URLdata));
- returnURL->protocol = NULL;
- returnURL->username = NULL;
- returnURL->password = NULL;
- returnURL->host = NULL;
- returnURL->port = NULL;
- returnURL->path = NULL;
- if ((postprotocol = strstr(weburl, "://")) == NULL) {
- return returnURL;
- }
- returnURL->protocol = malloc(sizeof(char) * (postprotocol - weburl + 1));
- strncpy(returnURL->protocol, weburl, postprotocol - weburl);
- postprotocol = (postprotocol + (*(postprotocol+3)=='/'?4:3));
- if ((postuserinfo = strstr(postprotocol, "@")) != NULL) {
- if ((postusername = strchr(postprotocol, ':')) != NULL) {
- returnURL->password = malloc(sizeof(char) * (postuserinfo - postusername + 1));
- strncpy(returnURL->password, postusername + 1, (postuserinfo - postusername - 1));
- }
- userlen = (postusername?postusername:postuserinfo) - postprotocol;
- returnURL->username = malloc(sizeof(char) * userlen + 1);
- strncpy(returnURL->username, postprotocol, userlen);
- }
- hostloc = (postuserinfo?postuserinfo + 1:postprotocol);
- if ((posthost = strchr(hostloc, '/')) == NULL) {
- posthost = hostloc + strlen(hostloc);
- isPath = 1;
- }
- if ((posthostname = strchr(hostloc, ':')) == NULL) {
- posthostname = posthost;
- }
- returnURL->host = malloc(sizeof(char) * (posthostname - hostloc + 1));
- strncpy(returnURL->host, hostloc, (posthostname - hostloc));
- if (posthostname != posthost) {
- posthostname++;
- returnURL->port = malloc(sizeof(char) * (posthost - posthostname + 1));
- strncpy(returnURL->port, posthostname, (posthost - posthostname));
- } else {
- returnURL->port = malloc(sizeof(char) * 2 + 1);
- strncpy(returnURL->port, "80", 2);
- }
- if (isPath) {
- returnURL->path = malloc(sizeof(char) * strlen(posthost) + 1);
- strncpy(returnURL->path, posthost, strlen(posthost));
- } else {
- returnURL->path = calloc(1, 1);
- }
- return returnURL;
- }
- void freeURLdata(URLdata *freeURL) {
- free(freeURL->protocol);
- free(freeURL->username);
- free(freeURL->password);
- free(freeURL->host);
- free(freeURL->port);
- free(freeURL->path);
- free(freeURL);
- }
RAW Paste Data