/* Some code that can be used to parse URLS (URIS). */ /* No guarantee that this code works on all URIs or that it will be a big help. I grabbed this from an old program and I believe that it works ( I tested and it seems to work), but there is no guarantee it will handle all URIs correctly !!! I don't have time to go through and review this code completely or to make sure that the comments make sense - a number of people have asked for this to I'm providing it ASAP. A few people have used this without much problem (understanding it)... */ typedef struct url { int proto; char host[100]; int port; char file[1000]; } url; /* split the line into tokens and return an array of tokens in d */ /* return value is the number of tokens found */ /* the line itself is corrupted (replacing whitespace with nulls) although the tokens remain and the array is just a bunch of pointers into the original string. d must be allocated and contain at least maxtok slots if maxtok is not large enough - we simply ignore any remaining tokens */ int split_line( char *s, char **d, int maxtok ) { char *line=s; int i=0; int done=0; int len; char *ws=" \t\n\r"; /* skip initial ws */ len = strspn(line,ws); line+=len; while (done==0) { /* find length of next token */ len=strcspn(line,ws); if (len!=0) { /* point to the token */ d[i++]=line; /* if the last thing in the line we are done */ if (line[len]==0) { done=1; } else { /* terminate the line */ line[len]=0; /* skip ws */ line+=len+1; line += strspn(line,ws); } /* check for too many tokens */ if (i==maxtok) { done=1; } } else { done=1; } } return(i); } /* get the protocol, hostname port number and filename from a URL */ url * parse_request(char *s ) { static url u; int j; char *p; char tmp[100]; if (strncasecmp(s,"http://",7) != 0) { u.proto=0; printf("Invalid URL: %s\n",s); return(&u); } u.proto=1; p = s+7; j=0; while ((*p!=0)&&(*p!=':')&&(*p!='/')) { u.host[j++]=*p++; } u.host[j]=0; if (*p==':') { j=0; p++; while ((*p)&&(*p!='/')) { tmp[j++]=*p++; } tmp[j]=0; u.port = atoi(tmp); printf("PORT IS (%s) %d\n",tmp,u.port); } else { u.port=80; } j=0; while (*p) { u.file[j++] = *p++; } u.file[j]=0; return(&u); }