Commit b223a43c317ad9b16d77a16c12af6403cb032e8b
1 parent
6f771698
add missing data attribute which contain phone number
remove force_nth which is now in anubis standard library
Showing
1 changed file
with
305 additions
and
305 deletions
Show diff stats
web/CXM_http_get_common.anubis
| 1 | - | ||
| 2 | - *Project* The Anubis Project | ||
| 3 | - | ||
| 4 | - *Title* Getting a document from the Web. | ||
| 5 | - | ||
| 6 | - *Copyright* Copyright (c) Alain Prouté 2001. | ||
| 7 | - | ||
| 8 | - | ||
| 9 | - *Author* Alain Prouté | ||
| 10 | - | ||
| 11 | - | ||
| 12 | - | ||
| 13 | - This file contains the types and functions which are common to 'http_get' and | ||
| 14 | - 'https_get'. | ||
| 15 | - | ||
| 16 | -read tools/basis.anubis | ||
| 17 | -read tools/findstring.anubis | ||
| 18 | -read system/string.anubis | ||
| 19 | -read CXM_common.anubis | ||
| 20 | - read web/html.anubis | ||
| 21 | - | ||
| 22 | - | ||
| 23 | - | ||
| 24 | - Body of HTTP requests may also contain name-value pairs. We call them HTTP arguments: | ||
| 25 | - | ||
| 26 | -public type HTTP_argument: | ||
| 27 | - http_argument(String name, String value). | ||
| 28 | - | ||
| 29 | - | ||
| 30 | - | ||
| 31 | - We begin by several simple tools. | ||
| 32 | - | ||
| 33 | - 'crlf' and 'crlfcrlf' are defined in 'basis.anubis'. | ||
| 34 | - | ||
| 35 | -public define Maybe(Int) | ||
| 36 | - has_double_crlf | ||
| 37 | - ( | ||
| 38 | - String s, | ||
| 39 | - ) = | ||
| 40 | - find(crlfcrlf,s,0). | ||
| 41 | - | ||
| 42 | - | ||
| 43 | - | ||
| 44 | - | ||
| 45 | - Formatting a list of HTTP arguments. | ||
| 46 | - | ||
| 47 | -public define String | ||
| 48 | - format_http_args | ||
| 49 | - ( | ||
| 50 | - List(HTTP_argument) l | ||
| 51 | - ) = | ||
| 52 | - if l is | ||
| 53 | - { | ||
| 54 | - [ ] then "", | ||
| 55 | - [h . t] then | ||
| 56 | - if t is | ||
| 57 | - { | ||
| 58 | - [ ] then if h is http_argument(n,v) then n + "=" + v, | ||
| 59 | - [_ . _] then if h is http_argument(n,v) then n + "=" + v + "&" + format_http_args(t) | ||
| 60 | - } | ||
| 61 | - }. | ||
| 62 | - | ||
| 63 | - | ||
| 64 | - | ||
| 65 | - Formatting the headers. | ||
| 66 | - | ||
| 67 | -public define String | ||
| 68 | - format_headers | ||
| 69 | - ( | ||
| 70 | - List(HTTP_header) headers | ||
| 71 | - ) = | ||
| 72 | - if headers is | ||
| 73 | - { | ||
| 74 | - [ ] then "", | ||
| 75 | - [h . t] then if h is http_header(n,v) then | ||
| 76 | - n + ": " + v + crlf + format_headers(t) | ||
| 77 | - }. | ||
| 78 | - | ||
| 79 | - | ||
| 80 | - | ||
| 81 | - | ||
| 82 | - | ||
| 83 | -public define String | ||
| 84 | - format | ||
| 85 | - ( | ||
| 86 | - DNS_Result r | ||
| 87 | - ) = | ||
| 88 | - if r is | ||
| 89 | - { | ||
| 90 | - host_not_found then "host not found", | ||
| 91 | - no_address_found then "no address found for this host", | ||
| 92 | - try_again then "DNS server is busy, try again later", | ||
| 93 | - non_recoverable_error then "non recoverable DNS error", | ||
| 94 | - ok(n) then should_not_happen("ok") // should never happen | ||
| 95 | - }. | ||
| 96 | - | ||
| 97 | - | ||
| 98 | -public define String | ||
| 99 | - format | ||
| 100 | - ( | ||
| 101 | - NetworkConnectError e | ||
| 102 | - ) = | ||
| 103 | - if e is | ||
| 104 | - { | ||
| 105 | - cannot_create_the_socket then "cannot create the socket", | ||
| 106 | - address_port_not_available then "address:port not available", | ||
| 107 | - connection_refused then "connection refused", | ||
| 108 | - network_unreachable then "network is unreachable", | ||
| 109 | - address_port_already_in_use then "address:port already in use", | ||
| 110 | - out_of_time then "out of time" | ||
| 111 | - }. | ||
| 112 | - | ||
| 113 | -public define String | ||
| 114 | - format | ||
| 115 | - ( | ||
| 116 | - SSLConnectError e | ||
| 117 | - ) = | ||
| 118 | - if e is | ||
| 119 | - { | ||
| 120 | - tcp_error(nce) then format(nce), | ||
| 121 | - cannot_create_SSL_object then "cannot create SSL object", | ||
| 122 | - cannot_connect_under_SSL then "cannot connect under SSL", | ||
| 123 | - cannot_trust_server_certificate then "cannot trust server certificate" | ||
| 124 | - }. | ||
| 125 | - | ||
| 126 | - | ||
| 127 | - | ||
| 128 | - The next function empties the standard input (the keybord normally). | ||
| 129 | - | ||
| 130 | -define One | ||
| 131 | - empty_stdin | ||
| 132 | - ( | ||
| 133 | - One dummy | ||
| 134 | - ) = | ||
| 135 | - if *stdin is | ||
| 136 | - { | ||
| 137 | - failure then unique, | ||
| 138 | - success(c) then | ||
| 139 | - if c = 10 | ||
| 140 | - then unique | ||
| 141 | - else empty_stdin(dummy) | ||
| 142 | - }. | ||
| 143 | - | ||
| 144 | - | ||
| 145 | - The next function returns true if the user answers Y or y. | ||
| 146 | - | ||
| 147 | -public define Bool | ||
| 148 | - yes | ||
| 149 | - = | ||
| 150 | - if *stdin is | ||
| 151 | - { | ||
| 152 | - failure then should_not_happen(false), // this can never happen, because stdin is never closed | ||
| 153 | - success(c) then | ||
| 154 | - empty_stdin(unique); | ||
| 155 | - (c = 'Y' | c = 'y') | ||
| 156 | - }. | ||
| 157 | - | ||
| 158 | - | ||
| 159 | - | ||
| 160 | - Separating the server name from the port. | ||
| 161 | - | ||
| 162 | -public define (String,Word32) | ||
| 163 | - separate_name_port | ||
| 164 | - ( | ||
| 165 | - String server_name, | ||
| 166 | - Word32 default_port | ||
| 167 | - ) = | ||
| 168 | - if find(":",server_name,0) is | ||
| 169 | - { | ||
| 170 | - failure then (server_name,default_port), | ||
| 171 | - success(n) then | ||
| 172 | - (substr(server_name,0,n), | ||
| 173 | - if decimal_scan(substr(server_name,n+1,length(server_name)-n-1)) is | ||
| 174 | - { | ||
| 175 | - failure then default_port, | ||
| 176 | - success(p) then truncate_to_Word32(p) | ||
| 177 | - }) | ||
| 178 | - }. | ||
| 179 | - | ||
| 180 | - | ||
| 181 | - | ||
| 182 | - | ||
| 183 | - | ||
| 184 | - Separating the headers. The headers are comming in the form of a unique string. They | ||
| 185 | - should be put in the form of a list of pairs of strings (one list element per header). | ||
| 186 | - | ||
| 187 | - We do that in two steps. (1) separate the headers into a list of strings (one per | ||
| 188 | - header), and (2) separate each string into a pair of strings (name,value). | ||
| 189 | - | ||
| 190 | - | ||
| 191 | -define HTTP_header | ||
| 192 | - separate_header | ||
| 193 | - ( | ||
| 194 | - String header, | ||
| 195 | - Int i | ||
| 196 | - ) = | ||
| 197 | - if nth(i,header) is | ||
| 198 | - { | ||
| 199 | - failure then http_header(header,""), | ||
| 200 | - success(c) then | ||
| 201 | - if c = ':' // separator | ||
| 202 | - then http_header(substr(header,0,i),substr(header,i+1,length(header)-i-1)) | ||
| 203 | - else separate_header(header,i+1) | ||
| 204 | - }. | ||
| 205 | - | ||
| 206 | - | ||
| 207 | - Headers are separated from each other by CRLF not followed by a blank character. | ||
| 208 | - | ||
| 209 | -define List(HTTP_header) | ||
| 210 | - separate_headers | ||
| 211 | - ( | ||
| 212 | - String headers, | ||
| 213 | - Int start, | ||
| 214 | - ) = | ||
| 215 | - if find("\r\n",headers,start) is | ||
| 216 | - { | ||
| 217 | - failure then [separate_header(substr(headers,start,length(headers)-start),0)], | ||
| 218 | - success(end) then | ||
| 219 | - [separate_header(substr(headers,start,end-start),0) . separate_headers(headers,end+2)] | ||
| 220 | - }. | ||
| 221 | - | ||
| 222 | -public define List(HTTP_header) | ||
| 223 | - separate_headers | ||
| 224 | - ( | ||
| 225 | - String headers, | ||
| 226 | - ) = | ||
| 227 | - separate_headers(headers,0). | ||
| 228 | - | ||
| 229 | - | ||
| 230 | -public define One | ||
| 231 | - print_headers | ||
| 232 | - ( | ||
| 233 | - List(HTTP_header) headers | ||
| 234 | - ) = | ||
| 235 | - if headers is | ||
| 236 | - { | ||
| 237 | - [ ] then unique, | ||
| 238 | - [h . t] then | ||
| 239 | - if h is http_header(l,r) then | ||
| 240 | - print(" "); print(l); print(": "); print(r); print("\n"); print_headers(t) | ||
| 241 | - }. | ||
| 242 | - | ||
| 243 | - | ||
| 244 | -public define Word8 | ||
| 245 | - force_nth | ||
| 246 | - ( | ||
| 247 | - Int n, | ||
| 248 | - String s | ||
| 249 | - ) = | ||
| 250 | - if nth(n,s) is | ||
| 251 | - { | ||
| 252 | - failure then 0, | ||
| 253 | - success(c) then c | ||
| 254 | - }. | ||
| 255 | - | ||
| 256 | - | ||
| 257 | -public define List(HTTP_header) | ||
| 258 | - get_headers | ||
| 259 | - ( | ||
| 260 | - List(String) args | ||
| 261 | - ) = | ||
| 262 | - if args is | ||
| 263 | - { | ||
| 264 | - [ ] then [ ], | ||
| 265 | - [h . t] then if nth(0,h) is | ||
| 266 | - { | ||
| 267 | - failure then should_not_happen([]), | ||
| 268 | - success(c) then if c = '=' | ||
| 269 | - then if t is | ||
| 270 | - { | ||
| 271 | - [ ] then [http_header(substr(h,1,length(h)-1),"")], | ||
| 272 | - [u . v] then [http_header(substr(h,1,length(h)-1),u) . get_headers(v)] | ||
| 273 | - } | ||
| 274 | - else get_headers(t) | ||
| 275 | - } | ||
| 276 | - }. | ||
| 277 | - | ||
| 278 | -public define List(HTTP_argument) | ||
| 279 | - get_arguments | ||
| 280 | - ( | ||
| 281 | - List(String) args | ||
| 282 | - ) = | ||
| 283 | - if args is | ||
| 284 | - { | ||
| 285 | - [ ] then [ ], | ||
| 286 | - [h . t] then if nth(0,h) is | ||
| 287 | - { | ||
| 288 | - failure then should_not_happen([]), | ||
| 289 | - success(c) then if c = '=' | ||
| 290 | - then if t is | ||
| 291 | - { | ||
| 292 | - [ ] then [ ], | ||
| 293 | - [u . v] then get_arguments(v) | ||
| 294 | - } | ||
| 295 | - else if c = '-' | ||
| 296 | - then get_arguments(t) | ||
| 297 | - else if t is | ||
| 298 | - { | ||
| 299 | - [ ] then [http_argument(h,"")], | ||
| 300 | - [u . v] then [http_argument(h,u) . get_arguments(v)] | ||
| 301 | - } | ||
| 302 | - } | ||
| 303 | - }. | ||
| 304 | - | ||
| 305 | - | 1 | + |
| 2 | + *Project* The Anubis Project | ||
| 3 | + | ||
| 4 | + *Title* Getting a document from the Web. | ||
| 5 | + | ||
| 6 | + *Copyright* Copyright (c) Alain Prouté 2001. | ||
| 7 | + | ||
| 8 | + | ||
| 9 | + *Author* Alain Prouté | ||
| 10 | + | ||
| 11 | + | ||
| 12 | + | ||
| 13 | + This file contains the types and functions which are common to 'http_get' and | ||
| 14 | + 'https_get'. | ||
| 15 | + | ||
| 16 | +read tools/basis.anubis | ||
| 17 | +read tools/findstring.anubis | ||
| 18 | +read system/string.anubis | ||
| 19 | +read CXM_common.anubis | ||
| 20 | + read web/html.anubis | ||
| 21 | + | ||
| 22 | + | ||
| 23 | + | ||
| 24 | + Body of HTTP requests may also contain name-value pairs. We call them HTTP arguments: | ||
| 25 | + | ||
| 26 | +public type HTTP_argument: | ||
| 27 | + http_argument(String name, String value). | ||
| 28 | + | ||
| 29 | + | ||
| 30 | + | ||
| 31 | + We begin by several simple tools. | ||
| 32 | + | ||
| 33 | + 'crlf' and 'crlfcrlf' are defined in 'basis.anubis'. | ||
| 34 | + | ||
| 35 | +public define Maybe(Int) | ||
| 36 | + has_double_crlf | ||
| 37 | + ( | ||
| 38 | + String s, | ||
| 39 | + ) = | ||
| 40 | + find(crlfcrlf,s,0). | ||
| 41 | + | ||
| 42 | + | ||
| 43 | + | ||
| 44 | + | ||
| 45 | + Formatting a list of HTTP arguments. | ||
| 46 | + | ||
| 47 | +public define String | ||
| 48 | + format_http_args | ||
| 49 | + ( | ||
| 50 | + List(HTTP_argument) l | ||
| 51 | + ) = | ||
| 52 | + if l is | ||
| 53 | + { | ||
| 54 | + [ ] then "", | ||
| 55 | + [h . t] then | ||
| 56 | + if t is | ||
| 57 | + { | ||
| 58 | + [ ] then if h is http_argument(n,v) then n + "=" + v, | ||
| 59 | + [_ . _] then if h is http_argument(n,v) then n + "=" + v + "&" + format_http_args(t) | ||
| 60 | + } | ||
| 61 | + }. | ||
| 62 | + | ||
| 63 | + | ||
| 64 | + | ||
| 65 | + Formatting the headers. | ||
| 66 | + | ||
| 67 | +public define String | ||
| 68 | + format_headers | ||
| 69 | + ( | ||
| 70 | + List(HTTP_header) headers | ||
| 71 | + ) = | ||
| 72 | + if headers is | ||
| 73 | + { | ||
| 74 | + [ ] then "", | ||
| 75 | + [h . t] then if h is http_header(n,v) then | ||
| 76 | + n + ": " + v + crlf + format_headers(t) | ||
| 77 | + }. | ||
| 78 | + | ||
| 79 | + | ||
| 80 | + | ||
| 81 | + | ||
| 82 | + | ||
| 83 | +public define String | ||
| 84 | + format | ||
| 85 | + ( | ||
| 86 | + DNS_Result r | ||
| 87 | + ) = | ||
| 88 | + if r is | ||
| 89 | + { | ||
| 90 | + host_not_found then "host not found", | ||
| 91 | + no_address_found then "no address found for this host", | ||
| 92 | + try_again then "DNS server is busy, try again later", | ||
| 93 | + non_recoverable_error then "non recoverable DNS error", | ||
| 94 | + ok(n) then should_not_happen("ok") // should never happen | ||
| 95 | + }. | ||
| 96 | + | ||
| 97 | + | ||
| 98 | +public define String | ||
| 99 | + format | ||
| 100 | + ( | ||
| 101 | + NetworkConnectError e | ||
| 102 | + ) = | ||
| 103 | + if e is | ||
| 104 | + { | ||
| 105 | + cannot_create_the_socket then "cannot create the socket", | ||
| 106 | + address_port_not_available then "address:port not available", | ||
| 107 | + connection_refused then "connection refused", | ||
| 108 | + network_unreachable then "network is unreachable", | ||
| 109 | + address_port_already_in_use then "address:port already in use", | ||
| 110 | + out_of_time then "out of time" | ||
| 111 | + }. | ||
| 112 | + | ||
| 113 | +public define String | ||
| 114 | + format | ||
| 115 | + ( | ||
| 116 | + SSLConnectError e | ||
| 117 | + ) = | ||
| 118 | + if e is | ||
| 119 | + { | ||
| 120 | + tcp_error(nce) then format(nce), | ||
| 121 | + cannot_create_SSL_object then "cannot create SSL object", | ||
| 122 | + cannot_connect_under_SSL then "cannot connect under SSL", | ||
| 123 | + cannot_trust_server_certificate then "cannot trust server certificate" | ||
| 124 | + }. | ||
| 125 | + | ||
| 126 | + | ||
| 127 | + | ||
| 128 | + The next function empties the standard input (the keybord normally). | ||
| 129 | + | ||
| 130 | +define One | ||
| 131 | + empty_stdin | ||
| 132 | + ( | ||
| 133 | + One dummy | ||
| 134 | + ) = | ||
| 135 | + if *stdin is | ||
| 136 | + { | ||
| 137 | + failure then unique, | ||
| 138 | + success(c) then | ||
| 139 | + if c = 10 | ||
| 140 | + then unique | ||
| 141 | + else empty_stdin(dummy) | ||
| 142 | + }. | ||
| 143 | + | ||
| 144 | + | ||
| 145 | + The next function returns true if the user answers Y or y. | ||
| 146 | + | ||
| 147 | +public define Bool | ||
| 148 | + yes | ||
| 149 | + = | ||
| 150 | + if *stdin is | ||
| 151 | + { | ||
| 152 | + failure then should_not_happen(false), // this can never happen, because stdin is never closed | ||
| 153 | + success(c) then | ||
| 154 | + empty_stdin(unique); | ||
| 155 | + (c = 'Y' | c = 'y') | ||
| 156 | + }. | ||
| 157 | + | ||
| 158 | + | ||
| 159 | + | ||
| 160 | + Separating the server name from the port. | ||
| 161 | + | ||
| 162 | +public define (String,Word32) | ||
| 163 | + separate_name_port | ||
| 164 | + ( | ||
| 165 | + String server_name, | ||
| 166 | + Word32 default_port | ||
| 167 | + ) = | ||
| 168 | + if find(":",server_name,0) is | ||
| 169 | + { | ||
| 170 | + failure then (server_name,default_port), | ||
| 171 | + success(n) then | ||
| 172 | + (substr(server_name,0,n), | ||
| 173 | + if decimal_scan(substr(server_name,n+1,length(server_name)-n-1)) is | ||
| 174 | + { | ||
| 175 | + failure then default_port, | ||
| 176 | + success(p) then truncate_to_Word32(p) | ||
| 177 | + }) | ||
| 178 | + }. | ||
| 179 | + | ||
| 180 | + | ||
| 181 | + | ||
| 182 | + | ||
| 183 | + | ||
| 184 | + Separating the headers. The headers are comming in the form of a unique string. They | ||
| 185 | + should be put in the form of a list of pairs of strings (one list element per header). | ||
| 186 | + | ||
| 187 | + We do that in two steps. (1) separate the headers into a list of strings (one per | ||
| 188 | + header), and (2) separate each string into a pair of strings (name,value). | ||
| 189 | + | ||
| 190 | + | ||
| 191 | +define HTTP_header | ||
| 192 | + separate_header | ||
| 193 | + ( | ||
| 194 | + String header, | ||
| 195 | + Int i | ||
| 196 | + ) = | ||
| 197 | + if nth(i,header) is | ||
| 198 | + { | ||
| 199 | + failure then http_header(header,""), | ||
| 200 | + success(c) then | ||
| 201 | + if c = ':' // separator | ||
| 202 | + then http_header(substr(header,0,i),substr(header,i+1,length(header)-i-1)) | ||
| 203 | + else separate_header(header,i+1) | ||
| 204 | + }. | ||
| 205 | + | ||
| 206 | + | ||
| 207 | + Headers are separated from each other by CRLF not followed by a blank character. | ||
| 208 | + | ||
| 209 | +define List(HTTP_header) | ||
| 210 | + separate_headers | ||
| 211 | + ( | ||
| 212 | + String headers, | ||
| 213 | + Int start, | ||
| 214 | + ) = | ||
| 215 | + if find("\r\n",headers,start) is | ||
| 216 | + { | ||
| 217 | + failure then [separate_header(substr(headers,start,length(headers)-start),0)], | ||
| 218 | + success(end) then | ||
| 219 | + [separate_header(substr(headers,start,end-start),0) . separate_headers(headers,end+2)] | ||
| 220 | + }. | ||
| 221 | + | ||
| 222 | +public define List(HTTP_header) | ||
| 223 | + separate_headers | ||
| 224 | + ( | ||
| 225 | + String headers, | ||
| 226 | + ) = | ||
| 227 | + separate_headers(headers,0). | ||
| 228 | + | ||
| 229 | + | ||
| 230 | +public define One | ||
| 231 | + print_headers | ||
| 232 | + ( | ||
| 233 | + List(HTTP_header) headers | ||
| 234 | + ) = | ||
| 235 | + if headers is | ||
| 236 | + { | ||
| 237 | + [ ] then unique, | ||
| 238 | + [h . t] then | ||
| 239 | + if h is http_header(l,r) then | ||
| 240 | + print(" "); print(l); print(": "); print(r); print("\n"); print_headers(t) | ||
| 241 | + }. | ||
| 242 | + | ||
| 243 | + | ||
| 244 | +//public define Word8 | ||
| 245 | +// force_nth | ||
| 246 | +// ( | ||
| 247 | +// Int n, | ||
| 248 | +// String s | ||
| 249 | +// ) = | ||
| 250 | +// if nth(n,s) is | ||
| 251 | +// { | ||
| 252 | +// failure then 0, | ||
| 253 | +// success(c) then c | ||
| 254 | +// }. | ||
| 255 | + | ||
| 256 | + | ||
| 257 | +public define List(HTTP_header) | ||
| 258 | + get_headers | ||
| 259 | + ( | ||
| 260 | + List(String) args | ||
| 261 | + ) = | ||
| 262 | + if args is | ||
| 263 | + { | ||
| 264 | + [ ] then [ ], | ||
| 265 | + [h . t] then if nth(0,h) is | ||
| 266 | + { | ||
| 267 | + failure then should_not_happen([]), | ||
| 268 | + success(c) then if c = '=' | ||
| 269 | + then if t is | ||
| 270 | + { | ||
| 271 | + [ ] then [http_header(substr(h,1,length(h)-1),"")], | ||
| 272 | + [u . v] then [http_header(substr(h,1,length(h)-1),u) . get_headers(v)] | ||
| 273 | + } | ||
| 274 | + else get_headers(t) | ||
| 275 | + } | ||
| 276 | + }. | ||
| 277 | + | ||
| 278 | +public define List(HTTP_argument) | ||
| 279 | + get_arguments | ||
| 280 | + ( | ||
| 281 | + List(String) args | ||
| 282 | + ) = | ||
| 283 | + if args is | ||
| 284 | + { | ||
| 285 | + [ ] then [ ], | ||
| 286 | + [h . t] then if nth(0,h) is | ||
| 287 | + { | ||
| 288 | + failure then should_not_happen([]), | ||
| 289 | + success(c) then if c = '=' | ||
| 290 | + then if t is | ||
| 291 | + { | ||
| 292 | + [ ] then [ ], | ||
| 293 | + [u . v] then get_arguments(v) | ||
| 294 | + } | ||
| 295 | + else if c = '-' | ||
| 296 | + then get_arguments(t) | ||
| 297 | + else if t is | ||
| 298 | + { | ||
| 299 | + [ ] then [http_argument(h,"")], | ||
| 300 | + [u . v] then [http_argument(h,u) . get_arguments(v)] | ||
| 301 | + } | ||
| 302 | + } | ||
| 303 | + }. | ||
| 304 | + | ||
| 305 | + |