summaryrefslogtreecommitdiffabout
path: root/src/util.cc
authorMichael Krelin <hacker@klever.net>2020-10-07 22:52:19 (UTC)
committer Michael Krelin <hacker@klever.net>2020-10-07 22:52:19 (UTC)
commitc561689bf162fb22997bd88f4392f222f151c950 (patch) (unidiff)
treece4656e92c379f7089f4bb72a4b10d781b61211e /src/util.cc
parent3a4530372bc95d728dbddbac788f2c1f2d03a030 (diff)
downloadkingate-c561689bf162fb22997bd88f4392f222f151c950.zip
kingate-c561689bf162fb22997bd88f4392f222f151c950.tar.gz
kingate-c561689bf162fb22997bd88f4392f222f151c950.tar.bz2
missing includesHEADmaster
Diffstat (limited to 'src/util.cc') (more/less context) (ignore whitespace changes)
-rw-r--r--src/util.cc1
1 files changed, 1 insertions, 0 deletions
diff --git a/src/util.cc b/src/util.cc
index 48e486a..76e684f 100644
--- a/src/util.cc
+++ b/src/util.cc
@@ -1,106 +1,107 @@
1#include <cstring>
1#include "kingate/util.h" 2#include "kingate/util.h"
2#include "kingate/exception.h" 3#include "kingate/exception.h"
3 4
4namespace kingate { 5namespace kingate {
5 6
6 static const char *safeChars = 7 static const char *safeChars =
7 "ABCDEFGHIJKLMNOPQRSTUVWXYZ" 8 "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
8 "abcdefghijklmnopqrstuvwxyz" 9 "abcdefghijklmnopqrstuvwxyz"
9 "0123456789" 10 "0123456789"
10 "_-" ; 11 "_-" ;
11 12
12 string url_encode(const string& str) { 13 string url_encode(const string& str) {
13 string rv = str; 14 string rv = str;
14 string::size_type screwed = 0; 15 string::size_type screwed = 0;
15 for(;;) { 16 for(;;) {
16 screwed = rv.find_first_not_of(safeChars,screwed); 17 screwed = rv.find_first_not_of(safeChars,screwed);
17 if(screwed == string::npos) 18 if(screwed == string::npos)
18 break; 19 break;
19 while(screwed<rv.length() && !strchr(safeChars,rv.at(screwed))) { 20 while(screwed<rv.length() && !strchr(safeChars,rv.at(screwed))) {
20 char danger = rv.at(screwed); 21 char danger = rv.at(screwed);
21 if(danger==' ') { 22 if(danger==' ') {
22 rv.replace(screwed++,1,1,'+'); 23 rv.replace(screwed++,1,1,'+');
23 }else{ 24 }else{
24 static char tmp[4] = {'%',0,0,0}; 25 static char tmp[4] = {'%',0,0,0};
25 snprintf(&tmp[1],3,"%02X",0xFF&(int)danger); 26 snprintf(&tmp[1],3,"%02X",0xFF&(int)danger);
26 rv.replace(screwed,1,tmp,3); 27 rv.replace(screwed,1,tmp,3);
27 screwed+=3; 28 screwed+=3;
28 } 29 }
29 } 30 }
30 } 31 }
31 return rv; 32 return rv;
32 } 33 }
33 string url_decode(const string& str) { 34 string url_decode(const string& str) {
34 string rv = str; 35 string rv = str;
35 string::size_type unscrewed = 0; 36 string::size_type unscrewed = 0;
36 for(;;) { 37 for(;;) {
37 unscrewed = rv.find_first_of("%+",unscrewed); 38 unscrewed = rv.find_first_of("%+",unscrewed);
38 if(unscrewed == string::npos) 39 if(unscrewed == string::npos)
39 break; 40 break;
40 if(rv.at(unscrewed)=='+') { 41 if(rv.at(unscrewed)=='+') {
41 rv.replace(unscrewed++,1,1,' '); 42 rv.replace(unscrewed++,1,1,' ');
42 }else{ 43 }else{
43 if((rv.length()-unscrewed)<3) 44 if((rv.length()-unscrewed)<3)
44 throw exception(CODEPOINT,"incorrectly escaped string"); 45 throw exception(CODEPOINT,"incorrectly escaped string");
45 // XXX: ensure it's hex? 46 // XXX: ensure it's hex?
46 int danger = strtol(rv.substr(unscrewed+1,2).c_str(),NULL,16); 47 int danger = strtol(rv.substr(unscrewed+1,2).c_str(),NULL,16);
47 rv.replace(unscrewed,3,1,danger); 48 rv.replace(unscrewed,3,1,danger);
48 unscrewed++; 49 unscrewed++;
49 } 50 }
50 } 51 }
51 return rv; 52 return rv;
52 } 53 }
53 54
54 /* 55 /*
55 * RFC 2616: 56 * RFC 2616:
56 * 57 *
57 * separators = "(" | ")" | "<" | ">" | "@" 58 * separators = "(" | ")" | "<" | ">" | "@"
58 * | "," | ";" | ":" | "\" | <"> 59 * | "," | ";" | ":" | "\" | <">
59 * | "/" | "[" | "]" | "?" | "=" 60 * | "/" | "[" | "]" | "?" | "="
60 * | "{" | "}" | SP | HT 61 * | "{" | "}" | SP | HT
61 */ 62 */
62 63
63 /* 64 /*
64 * RFC 2616: 65 * RFC 2616:
65 * 66 *
66 * token = 1*<any CHAR except CTLs or separators> 67 * token = 1*<any CHAR except CTLs or separators>
67 */ 68 */
68 69
69 static const char *quotible_chars = 70 static const char *quotible_chars =
70 "\001\002\003\004\005\006\007\010" 71 "\001\002\003\004\005\006\007\010"
71 "\011\012\013\014\015\016\017\020" 72 "\011\012\013\014\015\016\017\020"
72 "\021\022\023\024\025\026\027\030" 73 "\021\022\023\024\025\026\027\030"
73 "\031\032\033\034\035\036\037\040" 74 "\031\032\033\034\035\036\037\040"
74 "()<>@,;:\\\"/[]?={}" /* separator chars (except for SP and HT mentioned elsewhere */ 75 "()<>@,;:\\\"/[]?={}" /* separator chars (except for SP and HT mentioned elsewhere */
75 "\177" 76 "\177"
76 ; 77 ;
77 78
78 /* 79 /*
79 * RFC 2616: 80 * RFC 2616:
80 * 81 *
81 * quoted-string = ( <"> *(qdtext | quoted-pair ) <"> ) 82 * quoted-string = ( <"> *(qdtext | quoted-pair ) <"> )
82 * qdtext = <any TEXT except <">> 83 * qdtext = <any TEXT except <">>
83 * 84 *
84 * The backslash character ("\") MAY be used as a single-character 85 * The backslash character ("\") MAY be used as a single-character
85 * quoting mechanism only within quoted-string and comment constructs. 86 * quoting mechanism only within quoted-string and comment constructs.
86 * 87 *
87 * quoted-pair = "\" CHAR 88 * quoted-pair = "\" CHAR
88 */ 89 */
89 90
90 string http_quoted_string(const string& str) { 91 string http_quoted_string(const string& str) {
91 string rv = str; 92 string rv = str;
92 string::size_type sp=0; 93 string::size_type sp=0;
93 for(string::size_type q=rv.find('"');(q=rv.find('"',q))!=string::npos;q+=2) 94 for(string::size_type q=rv.find('"');(q=rv.find('"',q))!=string::npos;q+=2)
94 rv.insert(q,1,'\\'); 95 rv.insert(q,1,'\\');
95 rv.insert(0,1,'"'); 96 rv.insert(0,1,'"');
96 rv += '"'; 97 rv += '"';
97 return rv; 98 return rv;
98 } 99 }
99 100
100 string http_quote(const string& str) { 101 string http_quote(const string& str) {
101 if(str.find_first_of(quotible_chars)==string::npos) 102 if(str.find_first_of(quotible_chars)==string::npos)
102 return str; 103 return str;
103 return http_quoted_string(str); 104 return http_quoted_string(str);
104 } 105 }
105 106
106} 107}