BBS2chProxyHttpHeaders.h 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. #pragma once
  2. #include <map>
  3. #include <vector>
  4. #include <string>
  5. #include <set>
  6. #include <iterator>
  7. #if (defined(__clang__) && defined(_LIBCPP_VERSION)) || (__cplusplus >= 201103L)
  8. #include <memory>
  9. #else
  10. #include <tr1/memory>
  11. #endif
  12. #include <curl/curl.h>
  13. #ifdef USE_LUA
  14. #include <lua.hpp>
  15. #endif
  16. class BBS2chProxyHttpHeadersIterator;
  17. class BBS2chProxyHttpHeaderEntry {
  18. friend class BBS2chProxyHttpHeadersIterator;
  19. private:
  20. std::string _name;
  21. std::vector<std::string> _values;
  22. public:
  23. BBS2chProxyHttpHeaderEntry(const std::string &name, const std::string &value)
  24. {
  25. _name = name;
  26. _values.push_back(value);
  27. };
  28. const std::string& getName(void);
  29. std::string getLowercasedName(void);
  30. std::string getValue(void);
  31. std::string getFull(bool shouldIncludeLineBreak=false);
  32. std::vector<std::string>& getValueList(void);
  33. void add(const std::string &value);
  34. void set(const std::string &value);
  35. bool has(const std::string &value);
  36. bool contains(const std::string &value);
  37. void replaceValue(const std::string &from, const std::string &to);
  38. };
  39. #if (defined(__clang__) && defined(_LIBCPP_VERSION)) || (__cplusplus >= 201103L)
  40. typedef std::shared_ptr<BBS2chProxyHttpHeaderEntry> PBBS2chProxyHttpHeaderEntry;
  41. #else
  42. typedef std::tr1::shared_ptr<BBS2chProxyHttpHeaderEntry> PBBS2chProxyHttpHeaderEntry;
  43. #endif
  44. class BBS2chProxyHttpHeaders {
  45. friend class BBS2chProxyHttpHeadersIterator;
  46. private:
  47. std::map<std::string, PBBS2chProxyHttpHeaderEntry> _headers;
  48. std::string _statusLine;
  49. public:
  50. typedef BBS2chProxyHttpHeadersIterator iterator;
  51. std::string get(const std::string &name);
  52. std::string getFull(const std::string &name, bool shouldIncludeLineBreak=false);
  53. PBBS2chProxyHttpHeaderEntry getEntry(const std::string &name);
  54. bool has(const std::string &name);
  55. bool hasNameAndValue(const std::string &name, const std::string &value);
  56. void add(const std::string &name, const std::string &value);
  57. void add(const char *field);
  58. void add(const char *field, size_t length);
  59. void set(const std::string &name, const std::string &value);
  60. void set(PBBS2chProxyHttpHeaderEntry entry);
  61. void remove(const std::string &name);
  62. void clear(void);
  63. const std::string& getStatusLine();
  64. void setStatusLine(const char *field, size_t length);
  65. curl_slist* appendToCurlSlist(curl_slist *list, const std::set<std::string> &excludes = std::set<std::string>());
  66. curl_slist* appendToCurlSlist(curl_slist *list, const std::string &name);
  67. std::map<std::string, PBBS2chProxyHttpHeaderEntry>& getMap(void);
  68. bool empty();
  69. BBS2chProxyHttpHeaders::iterator begin();
  70. BBS2chProxyHttpHeaders::iterator end();
  71. static PBBS2chProxyHttpHeaderEntry parse(const char *field, size_t length);
  72. #ifdef USE_LUA
  73. static void getObjectMetatableForLua(lua_State *l);
  74. static void getClassDefinitionForLua(lua_State *l);
  75. void getUserdataForLua(lua_State *l);
  76. #endif
  77. };
  78. class BBS2chProxyHttpHeadersIterator {
  79. friend class BBS2chProxyHttpHeaders;
  80. std::pair<PBBS2chProxyHttpHeaderEntry, std::string> _value;
  81. std::map<std::string, PBBS2chProxyHttpHeaderEntry>::iterator _mapIterator;
  82. std::map<std::string, PBBS2chProxyHttpHeaderEntry>::iterator _mapIteratorEnd;
  83. std::vector<std::string>::iterator _vectorIterator;
  84. BBS2chProxyHttpHeadersIterator() {/* never used */};
  85. BBS2chProxyHttpHeadersIterator(BBS2chProxyHttpHeaders* headers, bool firstOrLast);
  86. public:
  87. typedef std::forward_iterator_tag iterator_categoly;
  88. typedef std::pair<PBBS2chProxyHttpHeaderEntry, std::string> value_type;
  89. typedef value_type* pointer;
  90. typedef value_type& reference;
  91. typedef ptrdiff_t difference_type; /* not implemented */
  92. BBS2chProxyHttpHeadersIterator& operator++();
  93. BBS2chProxyHttpHeadersIterator operator++(int);
  94. reference operator*();
  95. pointer operator->();
  96. bool operator==(const BBS2chProxyHttpHeadersIterator& iterator);
  97. bool operator!=(const BBS2chProxyHttpHeadersIterator& iterator);
  98. };