uri.c

Go to the documentation of this file.
00001 
00033 #include <stdlib.h>
00034 #include <stdio.h>
00035 #include <string.h>
00036 #include <ctype.h>
00037 
00038 #include "uri.h"
00039 
00048 void set_uri_port(uri_t* uri, char* port) {
00049         
00050         if(uri->port != NULL) {
00051                 free(uri->port);
00052         }
00053 
00054         if(!(uri->port = (char*)calloc((strlen(port) + 1), sizeof(char)))) {
00055                 printf("Could not alloc memory for uri->port!\n");
00056                 fflush(stdout);
00057         }
00058                 
00059         memcpy(uri->port, port, strlen(port));
00060 }
00061 
00070 void set_uri_passwd(uri_t* uri, char* passwd) {
00071         
00072         if(uri->passwd != NULL) {
00073                 free(uri->passwd);
00074         }
00075 
00076         if(!(uri->passwd = (char*)calloc((strlen(passwd) + 1), sizeof(char)))) {
00077                 printf("Could not alloc memory for uri->passwd!\n");
00078                 fflush(stdout);
00079         }
00080                 
00081         memcpy(uri->passwd, passwd, strlen(passwd));
00082 }
00083 
00092 void set_uri_params(uri_t* uri, char* params) {
00093         
00094         if(uri->params != NULL) {
00095                 free(uri->params);
00096         }
00097 
00098         if(!(uri->params = (char*)calloc((strlen(params) + 1), sizeof(char)))) {
00099                 printf("Could not alloc memory for uri->params!\n");
00100                 fflush(stdout);
00101         }
00102                 
00103         memcpy(uri->params, params, strlen(params));
00104 }
00105 
00114 void set_uri_query(uri_t* uri, char* query) {
00115         
00116         if(uri->query != NULL) {
00117                 free(uri->query);
00118         }
00119 
00120         if(!(uri->query = (char*)calloc((strlen(query) + 1), sizeof(char)))) {
00121                 printf("Could not alloc memory for uri->query!\n");
00122                 fflush(stdout);
00123         }
00124                 
00125         memcpy(uri->query, query, strlen(query));
00126 }
00127 
00136 void set_uri_frag(uri_t* uri, char* frag) {
00137         
00138         if(uri->frag != NULL) {
00139                 free(uri->frag);
00140         }
00141 
00142         if(!(uri->frag = (char*)calloc((strlen(frag) + 1), sizeof(char)))) {
00143                 printf("Could not alloc memory for uri->frag!\n");
00144                 fflush(stdout);
00145         }
00146                 
00147         memcpy(uri->frag, frag, strlen(frag));
00148 }
00149 
00161 char* parse_path(uri_t* uri, char* uri_pointer) {
00162 
00163         char *new_uri_pointer = NULL;
00164         char* start = uri_pointer;
00165         char* path_start = (char*)start;
00166         char* path_end;
00167 
00168         if((path_end = strchr(start, '#'))) {
00169                 *path_end = '\0';
00170                 
00171                 set_uri_path(uri, path_start);
00172                 new_uri_pointer = path_end + 1;
00173         }
00174         else {
00175                 set_uri_path(uri, start);
00176                 return NULL;
00177         }
00178 
00179         return new_uri_pointer;
00180 }
00181 
00193 char* parse_params(uri_t* uri, char* uri_pointer) {
00194 
00195         char *new_uri_pointer = NULL;
00196         char *scheme = NULL;
00197         int scheme_length = 0;
00198 
00199         char* start = uri_pointer;
00200         char* end;
00201 
00202         while(*start && isspace((int)*start)) {
00203                 start++;
00204         }
00205 
00206         end = start;
00207   
00208         while(*end && (isalnum((int)*end) || *end == '+' || *end == '-' || *end == '.')) {
00209                 end++;
00210         }
00211         
00212         if(*end != '\0' && end > start && *end == ':') {
00213                 scheme = start;
00214                 scheme_length = end - start;
00215                 scheme[scheme_length] = '\0';
00216         }
00217         else {
00218                 return NULL;
00219         }
00220 
00221         if(scheme_length != 0) {
00222                 
00223                 set_uri_scheme(uri, scheme);
00224                 new_uri_pointer = scheme + scheme_length + 1;           
00225         }
00226 
00227         return new_uri_pointer;
00228 }
00229 
00241 char* parse_query(uri_t* uri, char* uri_pointer) {
00242 
00243         char *new_uri_pointer = NULL;
00244         char *scheme = NULL;
00245         int scheme_length = 0;
00246 
00247         char* start = uri_pointer;
00248         char* end;
00249 
00250         while(*start && isspace((int)*start)) {
00251                 start++;
00252         }
00253 
00254         end = start;
00255   
00256         while(*end && (isalnum((int)*end) || *end == '+' || *end == '-' || *end == '.')) {
00257                 end++;
00258         }
00259         
00260         if(*end != '\0' && end > start && *end == ':') {
00261                 scheme = start;
00262                 scheme_length = end - start;
00263                 scheme[scheme_length] = '\0';
00264         }
00265         else {
00266                 return NULL;
00267         }
00268 
00269         if(scheme_length != 0) {
00270                 
00271                 set_uri_scheme(uri, scheme);
00272                 new_uri_pointer = scheme + scheme_length + 1;           
00273         }
00274 
00275         return new_uri_pointer;
00276 }
00277 
00289 char* parse_frag(uri_t* uri, char* uri_pointer) {
00290         
00291         set_uri_frag(uri, uri_pointer);
00292         
00293         return NULL;
00294 }
00295 
00307 char* parse_scheme(uri_t* uri, char* uri_pointer) {
00308 
00309         char *new_uri_pointer = NULL;
00310         char *scheme = NULL;
00311         int scheme_length = 0;
00312 
00313         char* start = uri_pointer;
00314         char* end;
00315 
00316         while(*start && isspace((int)*start)) {
00317                 start++;
00318         }
00319 
00320         end = start;
00321   
00322         while(*end && (isalnum((int)*end) || *end == '+' || *end == '-' || *end == '.')) {
00323                 end++;
00324         }
00325         
00326         if(*end != '\0' && end > start && *end == ':') {
00327                 scheme = start;
00328                 scheme_length = end - start;
00329                 scheme[scheme_length] = '\0';
00330         }
00331         else {
00332                 new_uri_pointer = uri_pointer;
00333         }
00334 
00335         if(scheme_length != 0) {
00336 
00337                 end = scheme;
00338   
00339                 while(*end) {
00340                         *end = tolower((int)*end);
00341                         end++;
00342                 }
00343 
00344                 set_uri_scheme(uri, scheme);
00345                 new_uri_pointer = scheme + scheme_length + 1;           
00346         }
00347 
00348         return new_uri_pointer;
00349 }
00350 
00362 char* parse_userinfo(uri_t *uri, char *uri_pointer) {
00363 
00364         char *new_uri_pointer = NULL;
00365         char* start = uri_pointer;
00366         char* end;
00367         char* user_start = start;
00368         char* user_end;
00369 
00370         if((end = strchr(start, '@'))) {
00371                 *end = '\0';
00372                         
00373                 if((user_end = strchr(user_start, ':'))) {
00374                         *user_end = '\0';
00375 
00376                         set_uri_user(uri, user_start);
00377                         set_uri_passwd(uri, (user_end + 1));
00378                 }
00379                 else {
00380                         set_uri_user(uri, start);
00381                 }
00382 
00383                 new_uri_pointer = end + 1;      
00384         }
00385         else {
00386                 new_uri_pointer = uri_pointer;  
00387         }
00388 
00389         return new_uri_pointer;
00390 }
00391 
00403 char* parse_hostport(uri_t* uri, char* uri_pointer) {
00404 
00405         char *new_uri_pointer = NULL;
00406         char* start;
00407         char* end;
00408         char* host_start;
00409         char* host_end;
00410 
00411         start = uri_pointer;
00412         end = start + strlen(uri_pointer);
00413         host_start = start;
00414 
00415         if((host_end = strchr(host_start, ':'))) {
00416                 *host_end = '\0';
00417                 set_uri_host(uri, host_start);
00418                 set_uri_port(uri, (host_end + 1));
00419         }
00420         else {
00421                 set_uri_host(uri, start);
00422         }
00423 
00424         new_uri_pointer = end + 1;      
00425 
00426         return new_uri_pointer;
00427 }
00428 
00440 char* parse_authority(uri_t* uri, char* uri_pointer) {
00441 
00442         char *new_uri_pointer = NULL;
00443         char *userinfo = NULL;
00444         char *hostport = NULL;
00445         char* start = uri_pointer;
00446         char* end;
00447 
00448         if(start[0] == '/' && start[1] == '/' && start[2] == '/') {
00449                 /* Authority part not present */
00450                 new_uri_pointer = start + 3;
00451         }
00452         else if(start[0] == '/' && start[1] == '/') {
00453                 /* Authority part present */
00454                         
00455                 start += 2;
00456                 userinfo = start;
00457                 end = start;
00458                         
00459                 while(*end && *end != '/') {
00460                         end++;
00461                 }
00462 
00463                 *end = '\0';
00464 
00465                 new_uri_pointer = end + 1;
00466                         
00467                 hostport = parse_userinfo(uri, userinfo);
00468                 new_uri_pointer = parse_hostport(uri, hostport);
00469 
00470         }
00471         else {
00472           new_uri_pointer = start;
00473         }
00474 
00475         return new_uri_pointer;
00476 }
00477 
00487 char* get_uri_host(uri_t* uri) {
00488         return uri->host;
00489 }
00490 
00500 char* get_uri_port(uri_t* uri) {
00501         return uri->port;
00502 }
00503 
00513 char* get_uri_path(uri_t* uri) {
00514         return uri->path;
00515 }
00516 
00526 char* get_uri_params(uri_t* uri) {
00527         return uri->params;
00528 }
00529 
00539 char* get_uri_query(uri_t* uri) {
00540         return uri->query;
00541 }
00542 
00552 char* get_uri_frag(uri_t* uri) {
00553         return uri->frag;
00554 }
00555 
00565 char* get_uri_user(uri_t* uri) {
00566         return uri->user;
00567 }
00568 
00578 char* get_uri_passwd(uri_t* uri) {
00579         return uri->passwd;
00580 }
00581 
00591 char* get_uri_scheme(uri_t* uri) {
00592         return uri->scheme;
00593 }
00594 
00595 uri_t* alloc_uri_struct(void) {
00596         
00597         uri_t *uri = NULL;
00598 
00599         if(!(uri = (uri_t*)calloc(1, sizeof(uri_t)))) {
00600                 printf("Could not alloc memory for uri structure!\n");
00601         }
00602 
00603         return uri;
00604 }
00605 
00606 uri_t* parse_uri(char* uri_string, int len) {
00607 
00608         uri_t* uri = NULL;
00609         char* tmp_uri = NULL;
00610         char* uri_pointer = NULL;
00611 
00612         if(!(tmp_uri = (char*)calloc((strlen(uri_string) + 1), sizeof(char)))) {
00613                 printf("Could not alloc memory for tmp (uri string)!\n");
00614                 return NULL;
00615         }
00616 
00617         memcpy(tmp_uri, uri_string, strlen(uri_string));
00618 
00619         if(!(uri = (uri_t*)calloc(1, sizeof(uri_t)))) {
00620                 printf("Could not alloc memory for uri structure!\n");
00621                 free(tmp_uri);
00622                 return NULL;
00623         }
00624 
00625         uri_pointer = parse_scheme(uri, tmp_uri);
00626 
00627         if(strcmp(uri_pointer, uri_string) == 0) {
00628         
00629                 uri_pointer = parse_path(uri, uri_pointer);
00630                 
00631                 if(uri_pointer != NULL) {
00632                         uri_pointer = parse_frag(uri, uri_pointer);
00633                 }
00634         }
00635         else {
00636                 uri_pointer = parse_authority(uri, uri_pointer);
00637                 uri_pointer = parse_path(uri, uri_pointer);
00638 
00639                 if(uri_pointer != NULL) {
00640                         uri_pointer = parse_frag(uri, uri_pointer);
00641                 }
00642         }
00643 
00644         free(tmp_uri);
00645         return uri;
00646 }
00647 
00648 char* uri_string(uri_t *uri) {
00649         
00650         char *uri_str;
00651         int len = 0;
00652 
00653         if(uri->scheme != NULL) {
00654                 len += strlen(uri->scheme);
00655                 len += 3; /* '://' */
00656         }
00657 
00658         if(strcmp(uri->scheme, "file") == 0) {
00659                 if(uri->host != NULL) {
00660                         len += strlen(uri->host);
00661                 }
00662                 if(uri->port != NULL) {
00663                         len += strlen(uri->port);
00664                         len += 1; /* ':' */
00665                 }
00666                 if(uri->path != NULL) {
00667                         len += 1; /* '/' */
00668                         len += strlen(uri->path);
00669                 }
00670                 /* TODO: add more */
00671         }
00672         if(strcmp(uri->scheme, "ftp") == 0) {
00673                 /* TODO */
00674         }
00675         if(strcmp(uri->scheme, "http") == 0) {
00676                 /* TODO */
00677         }
00678 
00679         if(!(uri_str = (char*)calloc((len + 1), sizeof(char)))) {
00680                 printf("Could not alloc memory for uri string!\n");
00681                 fflush(stdout);
00682                 return NULL;
00683         }
00684 
00685         if(uri->scheme != NULL) {
00686                 strcat(uri_str, uri->scheme);
00687                 strcat(uri_str, "://");
00688         }
00689 
00690         if(strcmp(uri->scheme, "file") == 0) {
00691                 if(uri->host != NULL) {
00692                         strcat(uri_str, uri->host);     
00693                 }
00694                 if(uri->port != NULL) {
00695                         strcat(uri_str, ":");
00696                         strcat(uri_str, uri->port);
00697                 }
00698                 if(uri->path != NULL) {
00699                         strcat(uri_str, "/");
00700                         strcat(uri_str, uri->path);
00701                 }
00702                 /* TODO: add more */
00703         }
00704         if(strcmp(uri->scheme, "ftp") == 0) {
00705                 /* TODO */
00706         }       
00707         if(strcmp(uri->scheme, "http") == 0) {
00708                 /* TODO */      
00709         }
00710 
00711     return uri_str;
00712 }
00713 
00714 void free_uri(uri_t* uri) {
00715 
00716         if(uri->scheme != NULL) {
00717                 free(uri->scheme);
00718         }
00719         if(uri->host != NULL) {
00720                 free(uri->host);
00721         }
00722         if(uri->port != NULL) {
00723                 free(uri->port);
00724         }
00725         if(uri->path != NULL) {
00726                 free(uri->path);
00727         }
00728         if(uri->params != NULL) {
00729                 free(uri->params);
00730         }
00731         if(uri->query != NULL) {
00732                 free(uri->query);
00733         }
00734         if(uri->frag != NULL) {
00735                 free(uri->frag);
00736         }
00737         if(uri->user != NULL) {
00738                 free(uri->user);
00739         }
00740         if(uri->passwd != NULL) {
00741                 free(uri->passwd);
00742         }
00743 
00744         free(uri);
00745 }
00746 
00747 char* get_uri_host_and_path(uri_t* uri) {
00748         
00749         char *tmp = NULL;
00750         int len = 0;
00751 
00752         if(uri->host != NULL) {
00753                 len += strlen(uri->host);
00754                 len += 1;
00755         }
00756         
00757         len += strlen(uri->path);
00758         
00759         if(!(tmp = (char*)calloc((len + 1), sizeof(char)))) {
00760                 printf("Could not alloc memory for tmp (get_uri_host_and_path)!\n");
00761                 fflush(stdout);
00762                 return NULL;
00763         }
00764         
00765         if(uri->host != NULL) {
00766                 strcat(tmp, uri->host);
00767                 strcat(tmp, "/");
00768         }
00769         
00770         strcat(tmp, uri->path);
00771 
00772         return tmp;
00773 }
00774 
00775 void set_uri_scheme(uri_t* uri, char* scheme) {
00776 
00777         if(uri->scheme != NULL) {
00778                 free(uri->scheme);
00779         }
00780 
00781         if(!(uri->scheme = (char*)calloc((strlen(scheme) + 1), sizeof(char)))) {
00782                 printf("Could not alloc memory for uri->scheme!\n");
00783                 fflush(stdout);
00784         }
00785                 
00786         memcpy(uri->scheme, scheme, strlen(scheme));
00787 }
00788 
00789 void set_uri_user(uri_t* uri, char* user) {
00790         
00791         if(uri->user != NULL) {
00792                 free(uri->user);
00793         }
00794 
00795         if(!(uri->user = (char*)calloc((strlen(user) + 1), sizeof(char)))) {
00796                 printf("Could not alloc memory for uri->user!\n");
00797                 fflush(stdout);
00798         }
00799                 
00800         memcpy(uri->user, user, strlen(user));
00801 }
00802 
00803 void set_uri_host(uri_t* uri, char* host) {
00804         
00805         if(uri->host != NULL) {
00806                 free(uri->host);
00807         }
00808 
00809         if(!(uri->host = (char*)calloc((strlen(host) + 1), sizeof(char)))) {
00810                 printf("Could not alloc memory for uri->host!\n");
00811                 fflush(stdout);
00812         }
00813                 
00814         memcpy(uri->host, host, strlen(host));
00815 }
00816 
00817 void set_uri_path(uri_t* uri, char* path) {
00818         
00819         if(uri->path != NULL) {
00820                 free(uri->path);
00821         }
00822 
00823         if(!(uri->path = (char*)calloc((strlen(path) + 1), sizeof(char)))) {
00824                 printf("Could not alloc memory for uri->path!\n");
00825                 fflush(stdout);
00826         }
00827                 
00828         memcpy(uri->path, path, strlen(path));
00829 }

Generated on Fri Mar 9 19:59:54 2007 for MAD-FCL by  doxygen 1.5.0