URL.h

Go to the documentation of this file.
00001 /*
00002  * Copyright (c) 2005 Palmsource, Inc.
00003  * 
00004  * This software is licensed as described in the file LICENSE, which
00005  * you should have received as part of this distribution. The terms
00006  * are also available at http://www.openbinder.org/license.html.
00007  * 
00008  * This software consists of voluntary contributions made by many
00009  * individuals. For the exact contribution history, see the revision
00010  * history and logs, available at http://www.openbinder.org
00011  */
00012 
00013 #ifndef _URL_H
00014 #define _URL_H
00015 
00021 #include <support/String.h>
00022 #include <support/Value.h>
00023 
00024 #if _SUPPORTS_NAMESPACE
00025 namespace palmos {
00026 namespace support {
00027 #endif
00028 
00033 struct URLData;
00034 
00035 class SUrl
00036 {
00037     public:
00038         SUrl();
00039         ~SUrl();
00040 
00047         SUrl(const SUrl&);
00048         //You should use this, if you know SUrl can understand this scheme
00049         SUrl(const char *urlString, bool escape_all = false);
00050 
00051         //hierarchal scheme's is followed by "://"
00052         //for example http://
00053 
00054         //hierarchal : need to be supplied only for custom schemes that is internally not supported
00055         //If a scheme is supported by SUrl, there is no need to supply hierarchal information
00056         SUrl(const char *scheme, const char *hostname, int port , bool escape_all = false, bool hierarchal =false );
00057         SUrl(const SUrl& baseURL, const char *relativePath, bool escape_all = false);
00058         SUrl(const SValue &value, status_t *status=NULL);
00059 
00060         bool IsValid() const;
00061         static bool IsSchemeSupported(const char *scheme);
00062 
00063 
00064 
00065         //-------------------------------------------------------------------
00066         //If Burl does not have a built in support for a scheme
00067         //  But if a scheme still follows the internet syntax for building Url
00068         //  we can help build the URL.
00069 
00070         //We build schemes like this
00071         //"scheme://<user>:<password>@<host>:<port>/<url-path>?<query>&<query>"
00072         //Any segement of URL is optional, except for scheme itself
00073         //  By default 
00074         //  ? is used to begin query (queryBeginChar)
00075         //  & is used to seperate two different queries (querySeperatorChar)
00076         //  = is used as assignment char is query is of form name=value (queryAssignmentChar)
00077         //-------------------------------------------------------------------
00078         status_t    SetInternetStyleSchemeSupport(char queryBeginChar = '?', 
00079                                                 char queryAssignmentChar = '=',
00080                                                 char querySeperatorChar = '&');
00081         //-------------------------------------------------------------------
00082 
00083         SString AsString() const;
00084         SValue AsValue() const;
00085         inline operator SValue() const { return AsValue(); }
00086 
00087         status_t SetTo(const SUrl& url);
00088         status_t SetTo(const char *urlString, bool escape_all = false);
00089         status_t SetTo(const SUrl& baseURL, const char *relativePath, bool escape_all = false);
00090         status_t SetTo(const SValue &value);
00091 
00096         bool Equals(const char*) const;
00097         bool Equals(const SUrl& url) const;
00098         bool operator==(const SUrl& url) const;
00099         bool operator==(const char *urlString) const;
00100         bool operator!=(const SUrl& url) const;
00101 
00103         SUrl& operator=(const char *url_string);
00104         SUrl& operator=(const SUrl& url);
00105 
00106         status_t    SetHostName(const char *hostname);
00107         status_t    SetUserName(const char *user);
00108         status_t    SetPassword(const char *password);
00109         status_t    SetFragment(const char *fragment);
00110 
00111         //Any string can be set as action. The scheme to which this Url belongs should
00112         //understand that action
00113 
00114         status_t    SetAction(const char *action);
00115         status_t    SetPath(const char *path, bool escape_all=false);
00116         status_t    SetPort(int port);
00117 
00118         const char* GetHostName() const;
00119         const char* GetUserName() const;
00120         const char* GetPassword() const;
00121         const char* GetFragment() const;
00122         const char *GetAction() const;
00123         const char* GetPath() const;
00124         unsigned short GetPort() const;
00125 
00126         const char* GetScheme() const;
00127 
00128         
00129         const char* GetExtension() const;
00130         const char* GetQuery() const;
00131 
00132 
00134         status_t SetQuery(const char *query, bool escape_all = false);
00135         status_t AddQueryParameter(const char *name, const char *value, bool escape_all = false);
00136         status_t ReplaceQueryParameter(const char* name, const char* value,
00137                 bool addIfNotPresent = true, bool escape_all = false);
00138         status_t RemoveQueryParameter(const char* name);
00139         status_t GetQueryParameter(const char* name, SString* out_value) const;
00140 
00141         
00153         void GetUnescapedFileName(char *out, int size) const;
00154         void GetUnescapedPath(char *out, int size) const;
00155         void GetUnescapedQuery(char *out, int size) const;
00156 
00157 
00158         void Reset();
00159 
00160 
00161 
00162 
00195         static char *EscapePathString(bool escape_all, char *outString, const char *inString,
00196                 size_t inLen = 0x7fffffff);
00197         static char *EscapeQueryString(bool escape_all, char *outString, const char *inString,
00198                 size_t inLen = 0x7fffffff);
00199 
00203         static int GetEscapedLength(const char *unescaped_string,size_t inLen = 0x7fffffff );
00204 
00205         void Print(SString *dump=NULL) const;
00206     private:
00207 
00208         //This will go away
00209         //SUrl(const char *scheme, const char *hostname, const char *path, int port,bool escape_all = false);
00210 
00211         //---------------------------------------------------------------------
00212         //Stuff thats probably not needed ..........
00213         //SUrl(const char *name, const SMessage *message);
00214         //void AddToMessage(const char *name, SMessage *message) const;
00215         //void ExtractFromMessage(const char *name, const SMessage *message);
00216         //uint32_t GenerateHash() const;
00217         //uint32_t HashString(const char *string, uint32_t hash = 0) const;
00218         //uint32_t HashStringI(const char *string, uint32_t hash = 0) const;
00219 
00220         
00221         //---------------------------------------------------------------------
00222 
00223 
00224         status_t ParseMailToURL(const char *src, bool escape_all);
00225         status_t ParseJavaScriptURL(const char *url);
00226         status_t ParseInternetURL(const char *_src, bool escape_all);
00227 
00228 
00229         status_t SetToInternal(const SUrl &base, const char *relativePath, bool escape_all);
00230         //int LookupScheme(const char *scheme);
00231         //int LookupScheme(const char *scheme, int len);
00232         char* NormalizePath();
00233         //void ParsePathURL(const char *url, bool escape_all);
00234 
00235         static char* EscapeString(bool escaped, char *outString, const char *inString, size_t inLen,
00236                 const unsigned int *validMap, const unsigned int *escapeMap);
00237 
00257         status_t SetNthString(int16_t setIndex, const char *newString, int32_t newStringLen = -1);
00258 
00259         status_t CreateUrlDataFromUrl(int32_t extraSpace, const URLData *refUrlData, const char *urlString, bool escape_all=false);
00260         status_t CreateUrlData(int32_t extraSpace, const char *scheme, const char* urlString, bool escape_all=false);
00261 
00262         status_t SetPathInternal(const char *path, int32_t length, bool escape_all);
00263 
00264         status_t SetQueryInternal(const char *query,int32_t length, bool escape_all);
00265 
00266         status_t GetQueryParameterInternal(const char *query, const char *name, 
00267                                          const char **namePP, int32_t *nameLengthP,
00268                                          const char **valuePP, int32_t *valueLengthP) const;
00269 
00270         status_t ComposeEscapedQuery(const char *name, const char *value, 
00271                                    bool escape_all, SString *outStrP) const ;
00272 
00273         status_t UnEscapedQuery(const char *query, int32_t length, SString *outStrP) const  ;
00274 
00275 
00276         //------------------------------------------------------------------
00277         URLData *fData;
00278         size_t  fDataLen;       // amount of 'fData' currently in use
00279         size_t  fExtraSpace;    // unused padding bytes at end of 'fData' buffer
00280 };
00281 
00284 #if _SUPPORTS_NAMESPACE
00285 } } // namespace palmos::support
00286 #endif
00287 
00288 #endif