llcorehttpheaders.cpp 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. /**
  2. * @file llcorehttpheaders.cpp
  3. * @brief Implementation of the HTTPHeaders class
  4. *
  5. * $LicenseInfo:firstyear=2012&license=viewerlgpl$
  6. * Second Life Viewer Source Code
  7. * Copyright (C) 2012-2013, Linden Research, Inc.
  8. *
  9. * This library is free software; you can redistribute it and/or
  10. * modify it under the terms of the GNU Lesser General Public
  11. * License as published by the Free Software Foundation;
  12. * version 2.1 of the License only.
  13. *
  14. * This library is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  17. * Lesser General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU Lesser General Public
  20. * License along with this library; if not, write to the Free Software
  21. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  22. *
  23. * Linden Research, Inc., 945 Battery Street, San Francisco, CA 94111 USA
  24. * $/LicenseInfo$
  25. */
  26. #include "linden_common.h"
  27. #include "llcorehttpheaders.h"
  28. namespace LLCore
  29. {
  30. void HttpHeaders::append(const std::string& name, const char* value)
  31. {
  32. for (S32 i = 0, count = mHeaders.size(); i < count; ++i)
  33. {
  34. if (mHeaders[i].first == name)
  35. {
  36. // This does happen in "normal" conditions...
  37. if (strcmp(mHeaders[i].second.c_str(), value) == 0)
  38. {
  39. return;
  40. }
  41. // This happens with cookies.
  42. mHeaders[i].second = value;
  43. return;
  44. }
  45. }
  46. mHeaders.emplace_back(name, value);
  47. }
  48. void HttpHeaders::append(const char* name, const char* value)
  49. {
  50. if (!name || !*name || !value || !*value) // Paranoia
  51. {
  52. return;
  53. }
  54. for (S32 i = 0, count = mHeaders.size(); i < count; ++i)
  55. {
  56. if (strcmp(mHeaders[i].first.c_str(), name) == 0)
  57. {
  58. // This does happen in "normal" conditions...
  59. if (strcmp(mHeaders[i].second.c_str(), value) == 0)
  60. {
  61. return;
  62. }
  63. // This happens with cookies.
  64. mHeaders[i].second = value;
  65. return;
  66. }
  67. }
  68. mHeaders.emplace_back(name, value);
  69. }
  70. void HttpHeaders::appendNormal(const char* header, size_t size)
  71. {
  72. std::string name;
  73. std::string value;
  74. size_t col_pos = 0;
  75. for ( ; col_pos < size; ++col_pos)
  76. {
  77. if (':' == header[col_pos])
  78. {
  79. break;
  80. }
  81. }
  82. if (col_pos < size)
  83. {
  84. // Looks like a header, split it and normalize.
  85. // Name is everything before the colon, may be zero-length.
  86. name.assign(header, col_pos);
  87. // Value is everything after the colon, may also be zero-length.
  88. const size_t val_len = size - col_pos - 1;
  89. if (val_len)
  90. {
  91. value.assign(header + col_pos + 1, val_len);
  92. }
  93. // Clean the strings
  94. LLStringUtil::toLower(name);
  95. LLStringUtil::trim(name);
  96. LLStringUtil::trimHead(value);
  97. }
  98. else
  99. {
  100. // Uncertain what this is, we'll pack it as a name without a value.
  101. // Won't clean as we don't know what it is...
  102. name.assign(header, size);
  103. }
  104. mHeaders.emplace_back(name, value);
  105. }
  106. // Find from end to simulate a tradition of using single-valued std::map for
  107. // this in the past.
  108. const std::string* HttpHeaders::find(const char* name) const
  109. {
  110. for (const_reverse_iterator it = rbegin(), iend = rend(); it != iend; ++it)
  111. {
  112. if (strcmp(it->first.c_str(), name) == 0)
  113. {
  114. return &it->second;
  115. }
  116. }
  117. return NULL;
  118. }
  119. // Find from end to simulate a tradition of using single-valued std::map for
  120. // this in the past.
  121. const std::string* HttpHeaders::find(const std::string& name) const
  122. {
  123. for (const_reverse_iterator it = rbegin(), iend = rend(); it != iend; ++it)
  124. {
  125. if (it->first == name)
  126. {
  127. return &it->second;
  128. }
  129. }
  130. return NULL;
  131. }
  132. void HttpHeaders::remove(const char* name)
  133. {
  134. for (iterator it = begin(), iend = end(); it != iend; ++it)
  135. {
  136. if (strcmp(it->first.c_str(), name) == 0)
  137. {
  138. mHeaders.erase(it);
  139. return;
  140. }
  141. }
  142. }
  143. } // End namespace LLCore