HttpHeaders.hpp 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. /*
  2. * Copyright (c) 2002-2009 Moxie Marlinspike
  3. *
  4. * This program is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU General Public License as
  6. * published by the Free Software Foundation; either version 3 of the
  7. * License, or (at your option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful, but
  10. * WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. * General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program; if not, write to the Free Software
  16. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
  17. * USA
  18. */
  19. #ifndef __HTTP_HEADERS_H__
  20. #define __HTTP_HEADERS_H__
  21. #define READING_ACTION 0
  22. #define READING_KEY 1
  23. #define READING_VALUE 2
  24. #define READING_POST 3
  25. #define READING_DONE 4
  26. #include <string>
  27. #include <map>
  28. #include <iostream>
  29. class HttpHeaderException : public std::exception {
  30. public:
  31. virtual const char* what() const throw() {
  32. return "HTTP Action Malformed...";
  33. }
  34. };
  35. /************************************************************************/
  36. /* Comparator for case-insensitive comparison in STL assos. containers */
  37. /************************************************************************/
  38. struct ci_less : std::binary_function<std::string, std::string, bool>
  39. {
  40. // case-independent (ci) compare_less binary function
  41. struct nocase_compare : public std::binary_function<unsigned char,unsigned char,bool>
  42. {
  43. bool operator() (const unsigned char& c1, const unsigned char& c2) const {
  44. return tolower (c1) < tolower (c2);
  45. }
  46. };
  47. bool operator() (const std::string & s1, const std::string & s2) const {
  48. return std::lexicographical_compare
  49. (s1.begin (), s1.end (), // source range
  50. s2.begin (), s2.end (), // dest range
  51. nocase_compare ()); // comparison
  52. }
  53. };
  54. class HttpHeaders {
  55. private:
  56. int foundCr;
  57. int foundLf;
  58. int state;
  59. std::string action;
  60. std::string method;
  61. std::string request;
  62. std::string postData;
  63. std::string key;
  64. std::string value;
  65. std::map<std::string, std::string, ci_less> headers;
  66. int readLine(char *buffer, int *offset, int length);
  67. int readAction(char *buffer, int length);
  68. int readValue(char *buffer, int offset, int length);
  69. int readKey(char *buffer, int offset, int length);
  70. void readPostData(char *buffer, int offset, int length);
  71. void parseAction();
  72. int getContentLength();
  73. public:
  74. HttpHeaders()
  75. : state(READING_ACTION),
  76. foundCr(0),
  77. foundLf(0) {}
  78. bool process(char * buffer, int length);
  79. bool isPost();
  80. std::map<std::string, std::string, ci_less>& getHeaders();
  81. std::string& getHeader(std::string& header);
  82. std::string& getMethod();
  83. std::string& getRequest();
  84. std::string& getPostData();
  85. };
  86. #endif