I wrote a simple URL parser using strtok (). here is the code
#include <stdio.h> #include <stdlib.h> typedef struct { char *protocol; char *host; int port; char *path; } aUrl; void parse_url(char *url, aUrl *ret) { printf("Parsing %s\n", url); char *tmp = (char *)_strdup(url); //char *protocol, *host, *port, *path; int len = 0; // protocol agora eh por exemplo http: ou https: ret->protocol = (char *) strtok(tmp, "/"); len = strlen(ret->protocol) + 2; ret->host = (char *) strtok(NULL, "/"); len += strlen(ret->host); //printf("char at %d => %c", len, url[len]); ret->path = (char *)_strdup(&url[len]); ret->path = (char *) strtok(ret->path, "#"); ret->protocol = (char *) strtok(ret->protocol, ":"); // host agora Γ© por exemplo address.com:8080 //tmp = (char *)_strdup(host); //strtok(tmp, ":"); ret->host = (char *) strtok(ret->host, ":"); tmp = (char *) strtok(NULL, ":"); if(tmp == NULL) { if(strcmp(ret->protocol, "http") == 0) { ret->port = 80; } else if(strcmp(ret->protocol, "https") == 0) { ret->port = 443; } } else { ret->port = atoi(tmp); } //host = (char *) strtok(NULL, "/"); } /* * */ int main(int argc, char** argv) { printf("hello moto\n"); aUrl myUrl; parse_url("http://teste.com/Teste/asdf#coisa", &myUrl); printf("protocol is %s\nhost is %s\nport is %d\npath is %s\n", myUrl.protocol, myUrl.host, myUrl.port, myUrl.path); return (EXIT_SUCCESS); }
As you can see, I use strtok () a lot, so I can "slice" the URL. I donβt need to support URLs other than http or https, so the way to solve it solves all my problems. It bothers me (it works on the embedded device) - Am I losing memory? When I write something like
ret->protocol = (char *) strtok(tmp, "/");
And then call
ret->protocol = (char *) strtok(ret->protocol, ":");
Does my first protocol keep the ret-> protocol in memory? I thought that maybe I need to set the first call to the tmp pointer, call strtok, pointing ret-> protocol to the right of the line (second call), and then free (tmp).
What should be the best way to use strtok?