Home | History | Annotate | Download | only in base
      1 /*
      2  *  Copyright 2004 The WebRTC Project Authors. All rights reserved.
      3  *
      4  *  Use of this source code is governed by a BSD-style license
      5  *  that can be found in the LICENSE file in the root of the source
      6  *  tree. An additional intellectual property rights grant can be found
      7  *  in the file PATENTS.  All contributing project authors may
      8  *  be found in the AUTHORS file in the root of the source tree.
      9  */
     10 
     11 #ifndef WEBRTC_BASE_HTTPCOMMON_INL_H__
     12 #define WEBRTC_BASE_HTTPCOMMON_INL_H__
     13 
     14 #include "webrtc/base/arraysize.h"
     15 #include "webrtc/base/common.h"
     16 #include "webrtc/base/httpcommon.h"
     17 
     18 namespace rtc {
     19 
     20 ///////////////////////////////////////////////////////////////////////////////
     21 // Url
     22 ///////////////////////////////////////////////////////////////////////////////
     23 
     24 template<class CTYPE>
     25 void Url<CTYPE>::do_set_url(const CTYPE* val, size_t len) {
     26   if (ascnicmp(val, "http://", 7) == 0) {
     27     val += 7; len -= 7;
     28     secure_ = false;
     29   } else if (ascnicmp(val, "https://", 8) == 0) {
     30     val += 8; len -= 8;
     31     secure_ = true;
     32   } else {
     33     clear();
     34     return;
     35   }
     36   const CTYPE* path = strchrn(val, len, static_cast<CTYPE>('/'));
     37   if (!path) {
     38     path = val + len;
     39   }
     40   size_t address_length = (path - val);
     41   do_set_address(val, address_length);
     42   do_set_full_path(path, len - address_length);
     43 }
     44 
     45 template<class CTYPE>
     46 void Url<CTYPE>::do_set_address(const CTYPE* val, size_t len) {
     47   if (const CTYPE* at = strchrn(val, len, static_cast<CTYPE>('@'))) {
     48     // Everything before the @ is a user:password combo, so skip it.
     49     len -= at - val + 1;
     50     val = at + 1;
     51   }
     52   if (const CTYPE* colon = strchrn(val, len, static_cast<CTYPE>(':'))) {
     53     host_.assign(val, colon - val);
     54     // Note: In every case, we're guaranteed that colon is followed by a null,
     55     // or non-numeric character.
     56     port_ = static_cast<uint16_t>(::strtoul(colon + 1, NULL, 10));
     57     // TODO: Consider checking for invalid data following port number.
     58   } else {
     59     host_.assign(val, len);
     60     port_ = HttpDefaultPort(secure_);
     61   }
     62 }
     63 
     64 template<class CTYPE>
     65 void Url<CTYPE>::do_set_full_path(const CTYPE* val, size_t len) {
     66   const CTYPE* query = strchrn(val, len, static_cast<CTYPE>('?'));
     67   if (!query) {
     68     query = val + len;
     69   }
     70   size_t path_length = (query - val);
     71   if (0 == path_length) {
     72     // TODO: consider failing in this case.
     73     path_.assign(1, static_cast<CTYPE>('/'));
     74   } else {
     75     ASSERT(val[0] == static_cast<CTYPE>('/'));
     76     path_.assign(val, path_length);
     77   }
     78   query_.assign(query, len - path_length);
     79 }
     80 
     81 template<class CTYPE>
     82 void Url<CTYPE>::do_get_url(string* val) const {
     83   CTYPE protocol[9];
     84   asccpyn(protocol, arraysize(protocol), secure_ ? "https://" : "http://");
     85   val->append(protocol);
     86   do_get_address(val);
     87   do_get_full_path(val);
     88 }
     89 
     90 template<class CTYPE>
     91 void Url<CTYPE>::do_get_address(string* val) const {
     92   val->append(host_);
     93   if (port_ != HttpDefaultPort(secure_)) {
     94     CTYPE format[5], port[32];
     95     asccpyn(format, arraysize(format), ":%hu");
     96     sprintfn(port, arraysize(port), format, port_);
     97     val->append(port);
     98   }
     99 }
    100 
    101 template<class CTYPE>
    102 void Url<CTYPE>::do_get_full_path(string* val) const {
    103   val->append(path_);
    104   val->append(query_);
    105 }
    106 
    107 template<class CTYPE>
    108 bool Url<CTYPE>::get_attribute(const string& name, string* value) const {
    109   if (query_.empty())
    110     return false;
    111 
    112   std::string::size_type pos = query_.find(name, 1);
    113   if (std::string::npos == pos)
    114     return false;
    115 
    116   pos += name.length() + 1;
    117   if ((pos > query_.length()) || (static_cast<CTYPE>('=') != query_[pos-1]))
    118     return false;
    119 
    120   std::string::size_type end = query_.find(static_cast<CTYPE>('&'), pos);
    121   if (std::string::npos == end) {
    122     end = query_.length();
    123   }
    124   value->assign(query_.substr(pos, end - pos));
    125   return true;
    126 }
    127 
    128 ///////////////////////////////////////////////////////////////////////////////
    129 
    130 }  // namespace rtc
    131 
    132 #endif  // WEBRTC_BASE_HTTPCOMMON_INL_H__
    133