// // IP gethostname // #include "netlib.h" #include "strlib.h" void uri_clear( uri_parts *parsed ) { bzero( parsed, sizeof(uri_parts) ); } void uri_defaults( uri_parts *parsed ) { bzero( parsed, sizeof(uri_parts) ); strcpy( parsed->proto, "http" ); strcpy( parsed->host, "localhost" ); strcpy( parsed->port, "80" ); strcpy( parsed->path, "/" ); } int uri_parse( const char *uri, uri_parts *parsed ) { char buf[4096]; char *proto = NULL, *host = NULL, *port = NULL; char *user = NULL, *pass = NULL, *path = NULL; char *s; int ret = 0; // output if ( !parsed ) return 0; // buffer strlcpy( buf, uri, sizeof(buf) ); // proto://user:pass@host:port/path s = buf; // proto if ( isalpha(*s) ) { s = strchr( buf, ':' ); if ( s ) { proto = buf; *s++ = '\0'; ret++; } else s = buf; } // absolute path if ( strncmp( s, "///", 3 ) == 0 ) { // skip over s += 2; // file path path = s++; ret++; } else if ( strncmp( s, "//", 2 ) == 0 ) { // skip over s += 2; // path if ( (path = strchr( s, '/' )) ) { *path++ = '\0'; strlcpy( parsed->path, "/", sizeof(parsed->path) ); ret++; } // user:pass, host if ( (host = strchr( s, '@' )) ) { // user user = s; *host++ = '\0'; ret++; // pass pass = strchr( user, ':' ); if ( pass ) { *pass++ = '\0'; ret++; } } else host = s; // host:port if ( strlen( host ) ) { ret++; port = strchr( host, ':' ); if ( port ) { *port++ = '\0'; ret++; } } parsed->rate = 0; } else if ( *s == '/' ) { // file path path = s; // the case of /dev/ttyS0:baud if ( (s = strchr( path, ':' )) ) { *s++ = '\0'; parsed->rate = atoi( s ); ret++; } ret++; } else { // hostname host = s; if ( (s = strchr( host, ':' )) ) { *s++ = '\0'; port = s; ret++; } ret++; } // copy if ( proto ) { strncpy( parsed->proto, proto, sizeof(parsed->proto) ); } if ( user ) { strncpy( parsed->user, user, sizeof(parsed->user) ); if ( pass ) strncpy( parsed->pass, pass, sizeof(parsed->pass) ); } if ( host ) { strncpy( parsed->host, host, sizeof(parsed->host) ); if ( port ) strncpy( parsed->port, port, sizeof(parsed->port) ); } if ( path ) strncat( parsed->path, path, sizeof(parsed->path)-1 ); return ret; }