00001 
00002 
00003 
00004 
00005 
00006 
00007 
00008 
00009 
00010 
00011 
00012 
00013 
00014 
00015 
00016 
00017 
00018 
00019 
00020 
00021 
00022 
00023 
00024 #ifndef __URL_H__
00025 #define __URL_H__
00026 
00027 #include "Arena.h"
00028 #include "HdrToken.h"
00029 #include "HdrHeap.h"
00030 #include "INK_MD5.h"
00031 #include "MMH.h"
00032 #include "MIME.h"
00033 
00034 #include "ink_apidefs.h"
00035 
00036 enum URLType
00037 {
00038   URL_TYPE_NONE,
00039   URL_TYPE_HTTP,
00040   URL_TYPE_HTTPS
00041 };
00042 
00043 struct URLImpl:public HdrHeapObjImpl
00044 {
00045   
00046   uint16_t m_len_scheme;
00047   uint16_t m_len_user;
00048   uint16_t m_len_password;
00049   uint16_t m_len_host;
00050   uint16_t m_len_port;
00051   uint16_t m_len_path;
00052   uint16_t m_len_params;
00053   uint16_t m_len_query;
00054   uint16_t m_len_fragment;
00055   uint16_t m_len_printed_string;
00056   
00057 
00058   const char *m_ptr_scheme;
00059   const char *m_ptr_user;
00060   const char *m_ptr_password;
00061   const char *m_ptr_host;
00062   const char *m_ptr_port;
00063   const char *m_ptr_path;
00064   const char *m_ptr_params;
00065   const char *m_ptr_query;
00066   const char *m_ptr_fragment;
00067   const char *m_ptr_printed_string;
00068   
00069 
00070   
00071   int16_t m_scheme_wks_idx;
00072   uint16_t m_port;
00073   uint8_t m_url_type;             
00074   uint8_t m_type_code;            
00075   
00076 
00077   uint32_t m_clean:1;
00078   
00079 
00080   
00081   int marshal(MarshalXlate *str_xlate, int num_xlate);
00082   void unmarshal(intptr_t offset);
00083   void move_strings(HdrStrHeap *new_heap);
00084   size_t strings_length();
00085 
00086   
00087   void check_strings(HeapCheck *heaps, int num_heaps);
00088 };
00089 
00090 
00091 
00092 
00093 
00094 class URLHashContext : public CryptoContext {
00095 public:
00096   URLHashContext();
00097 
00098   virtual bool update(void const* data, int length);
00099 
00100   virtual bool finalize(CryptoHash& hash);
00101 
00102   enum HashType { UNSPECIFIED, MD5, MMH }; 
00103   static HashType Setting;
00104 
00105 
00106   static size_t const OBJ_SIZE = 256;
00107 
00108 protected:
00109   char _obj[OBJ_SIZE]; 
00110 };
00111 
00112 inline bool URLHashContext::update(void const* data, int length) {
00113   return reinterpret_cast<CryptoContext*>(_obj)->update(data, length);
00114 }
00115 
00116 inline bool URLHashContext::finalize(CryptoHash& hash) {
00117   return reinterpret_cast<CryptoContext*>(_obj)->finalize(hash);
00118 }
00119 
00120 extern const char *URL_SCHEME_FILE;
00121 extern const char *URL_SCHEME_FTP;
00122 extern const char *URL_SCHEME_GOPHER;
00123 extern const char *URL_SCHEME_HTTP;
00124 extern const char *URL_SCHEME_HTTPS;
00125 extern const char *URL_SCHEME_WS;
00126 extern const char *URL_SCHEME_WSS;
00127 extern const char *URL_SCHEME_MAILTO;
00128 extern const char *URL_SCHEME_NEWS;
00129 extern const char *URL_SCHEME_NNTP;
00130 extern const char *URL_SCHEME_PROSPERO;
00131 extern const char *URL_SCHEME_TELNET;
00132 extern const char *URL_SCHEME_TUNNEL;
00133 extern const char *URL_SCHEME_WAIS;
00134 extern const char *URL_SCHEME_PNM;
00135 extern const char *URL_SCHEME_RTSP;
00136 extern const char *URL_SCHEME_RTSPU;
00137 extern const char *URL_SCHEME_MMS;
00138 extern const char *URL_SCHEME_MMSU;
00139 extern const char *URL_SCHEME_MMST;
00140 
00141 extern int URL_WKSIDX_FILE;
00142 extern int URL_WKSIDX_FTP;
00143 extern int URL_WKSIDX_GOPHER;
00144 extern int URL_WKSIDX_HTTP;
00145 extern int URL_WKSIDX_HTTPS;
00146 extern int URL_WKSIDX_WS;
00147 extern int URL_WKSIDX_WSS;
00148 extern int URL_WKSIDX_MAILTO;
00149 extern int URL_WKSIDX_NEWS;
00150 extern int URL_WKSIDX_NNTP;
00151 extern int URL_WKSIDX_PROSPERO;
00152 extern int URL_WKSIDX_TELNET;
00153 extern int URL_WKSIDX_TUNNEL;
00154 extern int URL_WKSIDX_WAIS;
00155 extern int URL_WKSIDX_PNM;
00156 extern int URL_WKSIDX_RTSP;
00157 extern int URL_WKSIDX_RTSPU;
00158 extern int URL_WKSIDX_MMS;
00159 extern int URL_WKSIDX_MMSU;
00160 extern int URL_WKSIDX_MMST;
00161 
00162 extern int URL_LEN_FILE;
00163 extern int URL_LEN_FTP;
00164 extern int URL_LEN_GOPHER;
00165 extern int URL_LEN_HTTP;
00166 extern int URL_LEN_HTTPS;
00167 extern int URL_LEN_WS;
00168 extern int URL_LEN_WSS;
00169 extern int URL_LEN_MAILTO;
00170 extern int URL_LEN_NEWS;
00171 extern int URL_LEN_NNTP;
00172 extern int URL_LEN_PROSPERO;
00173 extern int URL_LEN_TELNET;
00174 extern int URL_LEN_TUNNEL;
00175 extern int URL_LEN_WAIS;
00176 extern int URL_LEN_PNM;
00177 extern int URL_LEN_RTSP;
00178 extern int URL_LEN_RTSPU;
00179 extern int URL_LEN_MMS;
00180 extern int URL_LEN_MMSU;
00181 extern int URL_LEN_MMST;
00182 
00183 
00184 void url_adjust(MarshalXlate *str_xlate, int num_xlate);
00185 
00186 
00187 void url_init();
00188 
00189 URLImpl *url_create(HdrHeap *heap);
00190 void url_clear(URLImpl *url_impl);
00191 void url_nuke_proxy_stuff(URLImpl *d_url);
00192 
00193 URLImpl *url_copy(URLImpl *s_url, HdrHeap *s_heap, HdrHeap *d_heap, bool inherit_strs = true);
00194 void url_copy_onto(URLImpl *s_url, HdrHeap *s_heap, URLImpl *d_url, HdrHeap *d_heap, bool inherit_strs = true);
00195 void url_copy_onto_as_server_url(URLImpl *s_url, HdrHeap *s_heap,
00196                                  URLImpl *d_url, HdrHeap *d_heap, bool inherit_strs = true);
00197 
00198 int url_print(URLImpl *u, char *buf, int bufsize, int *bufindex, int *dumpoffset);
00199 void url_describe(HdrHeapObjImpl *raw, bool recurse);
00200 
00201 int url_length_get(URLImpl *url);
00202 char *url_string_get(URLImpl *url, Arena *arena, int *length, HdrHeap *heap);
00203 void url_clear_string_ref(URLImpl *url);
00204 char *url_string_get_ref(HdrHeap *heap, URLImpl *url, int *length);
00205 void url_called_set(URLImpl *url);
00206 char *url_string_get_buf(URLImpl *url, char *dstbuf, int dstbuf_size, int *length);
00207 
00208 const char *url_scheme_get(URLImpl *url, int *length);
00209 void url_MD5_get(URLImpl *url, CryptoHash *md5);
00210 void url_host_MD5_get(URLImpl *url, CryptoHash *md5);
00211 const char *url_scheme_set(HdrHeap *heap, URLImpl *url,
00212                            const char *value, int value_wks_idx, int length, bool copy_string);
00213 
00214 
00215 void url_user_set(HdrHeap *heap, URLImpl *url, const char *value, int length, bool copy_string);
00216 void url_password_set(HdrHeap *heap, URLImpl *url, const char *value, int length, bool copy_string);
00217 void url_host_set(HdrHeap *heap, URLImpl *url, const char *value, int length, bool copy_string);
00218 void url_port_set(HdrHeap *heap, URLImpl *url, unsigned int port);
00219 
00220 
00221 void url_path_set(HdrHeap *heap, URLImpl *url, const char *value, int length, bool copy_string);
00222 
00223 void url_type_set(URLImpl *url, unsigned int type);
00224 
00225 
00226 void url_params_set(HdrHeap *heap, URLImpl *url, const char *value, int length, bool copy_string);
00227 void url_query_set(HdrHeap *heap, URLImpl *url, const char *value, int length, bool copy_string);
00228 void url_fragment_set(HdrHeap *heap, URLImpl *url, const char *value, int length, bool copy_string);
00229 
00230 MIMEParseResult url_parse(HdrHeap *heap, URLImpl *url, const char **start, const char *end, bool copy_strings);
00231 MIMEParseResult url_parse_no_path_component_breakdown(HdrHeap *heap, URLImpl *url,
00232                                                       const char **start, const char *end, bool copy_strings);
00233 MIMEParseResult url_parse_internet(HdrHeap *heap, URLImpl *url,
00234                                    const char **start, const char *end, bool copy_strings);
00235 MIMEParseResult url_parse_http(HdrHeap *heap, URLImpl *url, const char **start, const char *end, bool copy_strings);
00236 MIMEParseResult url_parse_http_no_path_component_breakdown(HdrHeap *heap, URLImpl *url,
00237                                                            const char **start, const char *end, bool copy_strings);
00238 
00239 char *url_unescapify(Arena *arena, const char *str, int length);
00240 
00241 void unescape_str(char *&buf, char *buf_e, const char *&str, const char *str_e, int &state);
00242 void unescape_str_tolower(char *&buf, char *end, const char *&str, const char *str_e, int &state);
00243 
00244 
00245 inline int
00246 url_canonicalize_port(int type, int port)
00247 {
00248   if (port == 0) {
00249     if (type == URL_TYPE_HTTP)
00250       port = 80;
00251     else if (type == URL_TYPE_HTTPS)
00252       port = 443;
00253   }
00254   return (port);
00255 }
00256 
00257 class URL:public HdrHeapSDKHandle
00258 {
00259 public:
00260   URLImpl *m_url_impl;
00261 
00262   URL();
00263   ~URL();
00264 
00265   int valid() const;
00266 
00267   void create(HdrHeap *h);
00268   void copy(const URL *url);
00269   void copy_shallow(const URL *url);
00270   void clear();
00271   void reset();
00272   
00273   void nuke_proxy_stuff();
00274 
00275   int print(char *buf, int bufsize, int *bufindex, int *dumpoffset);
00276 
00277   int length_get();
00278   void clear_string_ref();
00279   char *string_get(Arena *arena, int *length = NULL);
00280   char *string_get_ref(int *length = NULL);
00281   char *string_get_buf(char *dstbuf, int dsbuf_size, int *length = NULL);
00282   void hash_get(CryptoHash *md5);
00283   void host_hash_get(CryptoHash *md5);
00284 
00285   const char *scheme_get(int *length);
00286   int scheme_get_wksidx();
00287   void scheme_set(const char *value, int length);
00288 
00289   const char *user_get(int *length);
00290   void user_set(const char *value, int length);
00291   const char *password_get(int *length);
00292   void password_set(const char *value, int length);
00293   const char *host_get(int *length);
00294   void host_set(const char *value, int length);
00295   int port_get();
00296   int port_get_raw();
00297   void port_set(int port);
00298 
00299   const char *path_get(int *length);
00300   void path_set(const char *value, int length);
00301 
00302   int type_get();
00303   void type_set(int type);
00304 
00305   const char *params_get(int *length);
00306   void params_set(const char *value, int length);
00307   const char *query_get(int *length);
00308   void query_set(const char *value, int length);
00309   const char *fragment_get(int *length);
00310   void fragment_set(const char *value, int length);
00311 
00312   MIMEParseResult parse(const char **start, const char *end);
00313   MIMEParseResult parse(const char *str, int length);
00314   MIMEParseResult parse_no_path_component_breakdown(const char *str, int length);
00315 
00316 public:
00317   static char *unescapify(Arena *arena, const char *str, int length);
00318 
00319 private:
00320 
00321   
00322     URL(const URL & u);
00323     URL & operator =(const URL & u);
00324 };
00325 
00326 
00327 
00328 
00329 
00330 inline
00331 URL::URL()
00332   : m_url_impl(NULL)
00333 {
00334 }
00335 
00336 
00337 
00338 
00339 inline
00340 URL::~URL()
00341 {
00342 }
00343 
00344 
00345 
00346 
00347 inline int
00348 URL::valid() const
00349 {
00350   return (m_heap && m_url_impl);
00351 }
00352 
00353 
00354 
00355 
00356 inline void
00357 URL::create(HdrHeap *heap)
00358 {
00359   if (heap) {
00360     m_heap = heap;
00361   } else if (!m_heap) {
00362     m_heap = new_HdrHeap();
00363   }
00364 
00365   m_url_impl = url_create(m_heap);
00366 }
00367 
00368 
00369 
00370 
00371 inline void
00372 URL::copy(const URL *url)
00373 {
00374   ink_assert(url->valid());
00375   url_copy_onto(url->m_url_impl, url->m_heap, m_url_impl, m_heap);
00376 }
00377 
00378 
00379 
00380 
00381 inline void
00382 URL::copy_shallow(const URL *url)
00383 {
00384   ink_assert(url->valid());
00385   this->set(url);
00386   m_url_impl = url->m_url_impl;
00387 }
00388 
00389 
00390 
00391 
00392 inline void
00393 URL::clear()
00394 {
00395   m_url_impl = NULL;
00396   HdrHeapSDKHandle::clear();
00397 }
00398 
00399 inline void
00400 URL::reset()
00401 {
00402   m_url_impl = NULL;
00403 }
00404 
00405 
00406 
00407 
00408 inline void
00409 URL::nuke_proxy_stuff()
00410 {
00411   ink_assert(valid());
00412   url_nuke_proxy_stuff(m_url_impl);
00413 }
00414 
00415 
00416 
00417 
00418 inline int
00419 URL::print(char *buf, int bufsize, int *bufindex, int *dumpoffset)
00420 {
00421   ink_assert(valid());
00422   return url_print(m_url_impl, buf, bufsize, bufindex, dumpoffset);
00423 }
00424 
00425 
00426 
00427 
00428 inline int
00429 URL::length_get()
00430 {
00431   ink_assert(valid());
00432   return url_length_get(m_url_impl);
00433 }
00434 
00435 
00436 
00437 
00438 inline char *
00439 URL::string_get(Arena *arena_or_null_for_malloc, int *length)
00440 {
00441   ink_assert(valid());
00442   return url_string_get(m_url_impl, arena_or_null_for_malloc, length, m_heap);
00443 }
00444 
00445 inline char *
00446 URL::string_get_ref(int *length)
00447 {
00448   ink_assert(valid());
00449   return url_string_get_ref(m_heap, m_url_impl, length);
00450 }
00451 
00452 inline void
00453 URL::clear_string_ref()
00454 {
00455   ink_assert(valid());
00456   url_clear_string_ref(m_url_impl);
00457   return;
00458 }
00459 
00460 
00461 
00462 inline char *
00463 URL::string_get_buf(char *dstbuf, int dsbuf_size, int *length)
00464 {
00465   ink_assert(valid());
00466   return url_string_get_buf(m_url_impl, dstbuf, dsbuf_size, length);
00467 }
00468 
00469 
00470 
00471 
00472 inline void
00473 URL::hash_get(CryptoHash *md5)
00474 {
00475   ink_assert(valid());
00476   url_MD5_get(m_url_impl, md5);
00477 }
00478 
00479 
00480 
00481 
00482 inline void
00483 URL::host_hash_get(CryptoHash *md5)
00484 {
00485   ink_assert(valid());
00486   url_host_MD5_get(m_url_impl, md5);
00487 }
00488 
00489 
00490 
00491 
00492 inline const char *
00493 URL::scheme_get(int *length)
00494 {
00495   ink_assert(valid());
00496   return (url_scheme_get(m_url_impl, length));
00497 }
00498 
00499 
00500 inline int
00501 URL::scheme_get_wksidx()
00502 {
00503   ink_assert(valid());
00504   return (m_url_impl->m_scheme_wks_idx);
00505 }
00506 
00507 
00508 
00509 
00510 inline void
00511 URL::scheme_set(const char *value, int length)
00512 {
00513   ink_assert(valid());
00514   int scheme_wks_idx = (value ? hdrtoken_tokenize(value, length) : -1);
00515   url_scheme_set(m_heap, m_url_impl, value, scheme_wks_idx, length, true);
00516 }
00517 
00518 
00519 
00520 
00521 inline const char *
00522 URL::user_get(int *length)
00523 {
00524   ink_assert(valid());
00525   *length = m_url_impl->m_len_user;
00526   return m_url_impl->m_ptr_user;
00527 }
00528 
00529 
00530 
00531 
00532 inline void
00533 URL::user_set(const char *value, int length)
00534 {
00535   ink_assert(valid());
00536   url_user_set(m_heap, m_url_impl, value, length, true);
00537 }
00538 
00539 
00540 
00541 
00542 inline const char *
00543 URL::password_get(int *length)
00544 {
00545   ink_assert(valid());
00546   *length = m_url_impl->m_len_password;
00547   return m_url_impl->m_ptr_password;
00548 }
00549 
00550 
00551 
00552 
00553 inline void
00554 URL::password_set(const char *value, int length)
00555 {
00556   ink_assert(valid());
00557   url_password_set(m_heap, m_url_impl, value, length, true);
00558 }
00559 
00560 
00561 
00562 
00563 inline const char *
00564 URL::host_get(int *length)
00565 {
00566   ink_assert(valid());
00567   *length = m_url_impl->m_len_host;
00568   return m_url_impl->m_ptr_host;
00569 }
00570 
00571 
00572 
00573 
00574 inline void
00575 URL::host_set(const char *value, int length)
00576 {
00577   ink_assert(valid());
00578   url_host_set(m_heap, m_url_impl, value, length, true);
00579 }
00580 
00581 
00582 
00583 
00584 inline int
00585 URL::port_get()
00586 {
00587   ink_assert(valid());
00588   return url_canonicalize_port(m_url_impl->m_url_type, m_url_impl->m_port);
00589 }
00590 
00591 
00592 
00593 
00594 inline int
00595 URL::port_get_raw()
00596 {
00597   ink_assert(valid());
00598   return m_url_impl->m_port;
00599 }
00600 
00601 
00602 
00603 
00604 inline void
00605 URL::port_set(int port)
00606 {
00607   ink_assert(valid());
00608   url_port_set(m_heap, m_url_impl, port);
00609 }
00610 
00611 
00612 
00613 
00614 inline const char *
00615 URL::path_get(int *length)
00616 {
00617   ink_assert(valid());
00618   *length = m_url_impl->m_len_path;
00619   return m_url_impl->m_ptr_path;
00620 }
00621 
00622 
00623 
00624 
00625 inline void
00626 URL::path_set(const char *value, int length)
00627 {
00628   ink_assert(valid());
00629   url_path_set(m_heap, m_url_impl, value, length, true);
00630 }
00631 
00632 
00633 
00634 
00635 inline int
00636 URL::type_get()
00637 {
00638   ink_assert(valid());
00639   return m_url_impl->m_type_code;
00640 }
00641 
00642 
00643 
00644 
00645 inline void
00646 URL::type_set(int type)
00647 {
00648   ink_assert(valid());
00649   url_type_set(m_url_impl, type);
00650 }
00651 
00652 
00653 
00654 
00655 inline const char *
00656 URL::params_get(int *length)
00657 {
00658   ink_assert(valid());
00659   *length = m_url_impl->m_len_params;
00660   return m_url_impl->m_ptr_params;
00661 }
00662 
00663 
00664 
00665 
00666 inline void
00667 URL::params_set(const char *value, int length)
00668 {
00669   ink_assert(valid());
00670   url_params_set(m_heap, m_url_impl, value, length, true);
00671 }
00672 
00673 
00674 
00675 
00676 inline const char *
00677 URL::query_get(int *length)
00678 {
00679   ink_assert(valid());
00680   *length = m_url_impl->m_len_query;
00681   return m_url_impl->m_ptr_query;
00682 }
00683 
00684 
00685 
00686 
00687 inline void
00688 URL::query_set(const char *value, int length)
00689 {
00690   ink_assert(valid());
00691   url_query_set(m_heap, m_url_impl, value, length, true);
00692 }
00693 
00694 
00695 
00696 
00697 inline const char *
00698 URL::fragment_get(int *length)
00699 {
00700   ink_assert(valid());
00701   *length = m_url_impl->m_len_fragment;
00702   return m_url_impl->m_ptr_fragment;
00703 }
00704 
00705 
00706 
00707 
00708 inline void
00709 URL::fragment_set(const char *value, int length)
00710 {
00711   ink_assert(valid());
00712   url_fragment_set(m_heap, m_url_impl, value, length, true);
00713 }
00714 
00715 
00716 
00717 
00718 
00719 
00720 inline MIMEParseResult
00721 URL::parse(const char **start, const char *end)
00722 {
00723   ink_assert(valid());
00724   return url_parse(m_heap, m_url_impl, start, end, true);
00725 }
00726 
00727 
00728 
00729 
00730 
00731 
00732 inline MIMEParseResult
00733 URL::parse(const char *str, int length)
00734 {
00735   ink_assert(valid());
00736   if (length < 0)
00737     length = (int) strlen(str);
00738   return parse(&str, str + length);
00739 }
00740 
00741 
00742 
00743 
00744 
00745 
00746 inline MIMEParseResult
00747 URL::parse_no_path_component_breakdown(const char *str, int length)
00748 {
00749   ink_assert(valid());
00750   if (length < 0)
00751     length = (int) strlen(str);
00752   ink_assert(valid());
00753   return url_parse_no_path_component_breakdown(m_heap, m_url_impl, &str, str + length, true);
00754 }
00755 
00756 
00757 
00758 
00759 inline char *
00760 URL::unescapify(Arena *arena, const char *str, int length)
00761 {
00762   return url_unescapify(arena, str, length);
00763 }
00764 
00765 #endif 
00766