HTTP Parser and message builder / objects in plain C Snapshot
uri.h
Go to the documentation of this file.
00001 #ifndef __URI_X_Y_Z_
00002 #define __URI_X_Y_Z_
00003 
00004 #include <cutils/base.h>
00005 #include <string.h>
00006 
00007 // ===============================================================
00008 
00009 /**
00010  * @defgroup URI
00011  * @brief uri parser
00012  *
00013  * @{
00014  */
00015 
00016 #define URI_FLAGS_PARSE_PARTS     1
00017 #define URI_FLAGS_IS_ABSOLUTE_URI 2
00018 #define URI_FLAGS_IS_OPAQUE       4
00019 #define URI_FLAGS_HOST_IPv4       8
00020 #define URI_FLAGS_HOST_IPv6       16
00021 #define URI_FLAGS_HOST_HOSTNAME   32
00022 #define URI_FLAGS_HAS_SCHEME      64
00023 
00024 
00025 
00026 typedef struct tagURI {
00027   int  flags;
00028 
00029   char *cdata_raw;
00030   char *cdata;
00031   char *cdata_is_escaped; // each bytes says if corresponding byte has been escaped. could do this as a bit array, alas.
00032 
00033   char *scheme;
00034 
00035   char *userinfo_raw;
00036   char *userinfo;
00037   
00038   char *host;
00039   int   port;
00040   
00041   char *path_raw;
00042   char *path;
00043 
00044   char *query_raw;
00045   char *query;
00046   
00047   char *fragment_raw;
00048   char *fragment;
00049 
00050   char *opaque_raw;
00051   char *opaque;
00052 
00053 } URI;
00054 
00055 M_INLINE void URI_init( URI *url )
00056 {
00057   memset( url, 0, sizeof( URI ) );
00058   url->port = -1;
00059 }
00060 
00061 
00062 
00063 int URI_parse( URI *url, char *line);
00064 
00065 M_INLINE void URI_free( URI *url) {
00066   if (url->cdata_raw) {
00067     free( url->cdata_raw );
00068   }
00069 
00070   if (url->cdata) {
00071     free(url->cdata);
00072   }
00073 
00074   if ( url->cdata_is_escaped ) {
00075     free( url->cdata_is_escaped );
00076   }
00077   URI_init( url );
00078 }
00079 
00080 M_INLINE const char *URI_scheme( URI *url )
00081 {
00082   return url->scheme;
00083 }
00084 
00085 M_INLINE const char *URI_userinfo_raw( URI *url )
00086 {
00087   return url->userinfo_raw;
00088 }
00089 
00090 M_INLINE const char *URI_userinfo( URI *url )
00091 {
00092   return url->userinfo;
00093 }
00094 
00095 M_INLINE const char *URI_host( URI *url )
00096 {
00097   return url->host;
00098 }
00099 
00100 M_INLINE int URI_port( URI *url )
00101 {
00102   return url->port;
00103 }
00104 
00105 
00106 M_INLINE const char *URI_path_raw( URI *url )
00107 {
00108   return url->path_raw;
00109 }
00110 
00111 M_INLINE const char *URI_path( URI *url )
00112 {
00113   return url->path;
00114 }
00115 
00116 M_INLINE const char *URI_query_raw( URI *url )
00117 {
00118   return url->query_raw;
00119 }
00120 
00121 M_INLINE const char *URI_query( URI *url )
00122 {
00123   return url->query;
00124 }
00125 
00126 M_INLINE const char *URI_fragment_raw( URI *url )
00127 {
00128   return url->fragment_raw;
00129 }
00130 
00131 M_INLINE const char *URI_fragment( URI *url )
00132 {
00133   return url->fragment;
00134 }
00135 
00136 M_INLINE const char *URI_opaque_raw( URI *url )
00137 {
00138   return url->opaque_raw;
00139 }
00140 
00141 M_INLINE const char *URI_opaque( URI *url )
00142 {
00143   return url->opaque;
00144 }
00145 
00146 /**
00147  * @}
00148  */
00149 
00150 #endif
00151