ChipMaster's trial hacks on C++CMS starting with v1.2.1. Not sure I'll follow on with the v2 since it looks to be breaking and mostly frivolous.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

99 lines
1.8 KiB

  1. #define CPPCMS_SOURCE
  2. #include "util.h"
  3. #include "http_protocol.h"
  4. #include <stdio.h>
  5. namespace cppcms {
  6. namespace util {
  7. std::string escape(std::string const &s)
  8. {
  9. std::string content;
  10. unsigned i,len=s.size();
  11. content.reserve(len*3/2);
  12. for(i=0;i<len;i++) {
  13. char c=s[i];
  14. switch(c){
  15. case '<': content+="&lt;"; break;
  16. case '>': content+="&gt;"; break;
  17. case '&': content+="&amp;"; break;
  18. case '\"': content+="&quot;"; break;
  19. default: content+=c;
  20. }
  21. }
  22. return content;
  23. }
  24. std::string urlencode(std::string const &s)
  25. {
  26. std::string content;
  27. unsigned i,len=s.size();
  28. content.reserve(3*len);
  29. for(i=0;i<len;i++){
  30. char c=s[i];
  31. if( ('a'<=c && c<='z')
  32. || ('A'<=c && c<='Z')
  33. || ('0'<=c && c<='9'))
  34. {
  35. content+=c;
  36. }
  37. else {
  38. switch(c) {
  39. case '-':
  40. case '_':
  41. case '.':
  42. case '~':
  43. content+=c;
  44. break;
  45. default:
  46. {
  47. char buf[4];
  48. #ifdef HAVE_SNPRINTF
  49. snprintf(buf,sizeof(buf),"%%%02x",(unsigned)(c));
  50. #else
  51. sprintf(buf,"%%%02x",(unsigned)(c));
  52. // Should be OK for this range
  53. #endif
  54. content.append(buf,3);
  55. }
  56. };
  57. }
  58. };
  59. return content;
  60. }
  61. std::string urldecode(std::string const &s)
  62. {
  63. return urldecode(s.c_str(),s.c_str()+s.size());
  64. }
  65. // TODO: Find correct RFC for proprer decoding
  66. std::string urldecode(char const *begin,char const *end)
  67. {
  68. std::string result;
  69. result.reserve(end-begin);
  70. for(;begin<end;begin++) {
  71. char c=*begin;
  72. switch(c) {
  73. case '+': result+=' ';
  74. break;
  75. case '%':
  76. if(end-begin >= 3 && http::protocol::xdigit(begin[1]) && http::protocol::xdigit(begin[2])) {
  77. char buf[3]={begin[1],begin[2],0};
  78. int value;
  79. sscanf(buf,"%x",&value);
  80. result+=char(value);
  81. begin+=2;
  82. }
  83. break;
  84. default:
  85. result+=c;
  86. }
  87. }
  88. return result;
  89. }
  90. } // util
  91. } // util