| 1 | #ifdef RCSID |
| 2 | static char RCSid[] = |
| 3 | "$Header$"; |
| 4 | #endif |
| 5 | |
| 6 | /* Copyright (c) 2010 by Michael J. Roberts. All Rights Reserved. */ |
| 7 | /* |
| 8 | Name |
| 9 | osifcnet.cpp - portable implementation for OS network interface |
| 10 | Function |
| 11 | |
| 12 | Notes |
| 13 | |
| 14 | Modified |
| 15 | 08/19/10 MJRoberts - Creation |
| 16 | */ |
| 17 | |
| 18 | #include "t3std.h" |
| 19 | #include "osifcnet.h" |
| 20 | #include "vmtype.h" |
| 21 | #include "vmdatasrc.h" |
| 22 | |
| 23 | /* ------------------------------------------------------------------------ */ |
| 24 | /* |
| 25 | * Payload item |
| 26 | */ |
| 27 | |
| 28 | /* payload item destruction */ |
| 29 | OS_HttpPayloadItem::~OS_HttpPayloadItem() |
| 30 | { |
| 31 | lib_free_str(name); |
| 32 | lib_free_str(val); |
| 33 | lib_free_str(mime_type); |
| 34 | if (stream != 0) |
| 35 | delete stream; |
| 36 | } |
| 37 | |
| 38 | /* create a payload item consisting of a simple name/value pair */ |
| 39 | OS_HttpPayloadItem::OS_HttpPayloadItem(const char *name, const char *val) |
| 40 | { |
| 41 | init(); |
| 42 | this->name = lib_copy_str(name); |
| 43 | this->val = lib_copy_str(val != 0 ? val : ""); |
| 44 | } |
| 45 | |
| 46 | /* create a payload item consisting of a simple name/value pair */ |
| 47 | OS_HttpPayloadItem::OS_HttpPayloadItem(const char *name, size_t name_len, |
| 48 | const char *val, size_t val_len) |
| 49 | { |
| 50 | init(); |
| 51 | this->name = lib_copy_str(name, name_len); |
| 52 | this->val = lib_copy_str(val, val_len); |
| 53 | } |
| 54 | |
| 55 | /* create a payload item for a PUT/POST file upload */ |
| 56 | OS_HttpPayloadItem::OS_HttpPayloadItem( |
| 57 | const char *name, const char *filename, |
| 58 | const char *mime_type, CVmDataSource *contents) |
| 59 | { |
| 60 | init(); |
| 61 | this->name = lib_copy_str(name); |
| 62 | this->val = lib_copy_str(filename != 0 ? filename : ""); |
| 63 | this->mime_type = lib_copy_str(mime_type != 0 ? mime_type : ""); |
| 64 | this->stream = contents; |
| 65 | } |
| 66 | |
| 67 | /* create a payload item for a PUT/POST file upload */ |
| 68 | OS_HttpPayloadItem::OS_HttpPayloadItem( |
| 69 | const char *name, size_t name_len, |
| 70 | const char *filename, size_t filename_len, |
| 71 | const char *mime_type, size_t mime_type_len, |
| 72 | CVmDataSource *contents) |
| 73 | { |
| 74 | init(); |
| 75 | this->name = lib_copy_str(name, name_len); |
| 76 | this->val = lib_copy_str(filename != 0 ? filename : "", filename_len); |
| 77 | this->mime_type = lib_copy_str( |
| 78 | mime_type != 0 ? mime_type : "", mime_type_len); |
| 79 | this->stream = contents; |
| 80 | } |
| 81 | |
| 82 | /* initialize - clear the fields */ |
| 83 | void OS_HttpPayloadItem::init() |
| 84 | { |
| 85 | name = 0; |
| 86 | val = 0; |
| 87 | stream = 0; |
| 88 | mime_type = 0; |
| 89 | nxt = 0; |
| 90 | } |
| 91 | |
| 92 | /* ------------------------------------------------------------------------ */ |
| 93 | /* |
| 94 | * Request payload data |
| 95 | */ |
| 96 | |
| 97 | |
| 98 | /* construction */ |
| 99 | OS_HttpPayload::OS_HttpPayload() |
| 100 | { |
| 101 | /* clear the item list initially */ |
| 102 | first_item = last_item = 0; |
| 103 | } |
| 104 | |
| 105 | /* destruction */ |
| 106 | OS_HttpPayload::~OS_HttpPayload() |
| 107 | { |
| 108 | /* delete our item list */ |
| 109 | OS_HttpPayloadItem *cur, *nxt; |
| 110 | for (cur = first_item ; cur != 0 ; cur = nxt) |
| 111 | { |
| 112 | nxt = cur->nxt; |
| 113 | delete cur; |
| 114 | } |
| 115 | } |
| 116 | |
| 117 | /* add a form field */ |
| 118 | void OS_HttpPayload::add(const char *name, const char *val) |
| 119 | { |
| 120 | add(new OS_HttpPayloadItem(name, val)); |
| 121 | } |
| 122 | |
| 123 | /* add a form field in TADS-string (VMB_LEN prefixed) format */ |
| 124 | void OS_HttpPayload::add_tstr(const char *name, const char *val) |
| 125 | { |
| 126 | add(new OS_HttpPayloadItem(name + VMB_LEN, vmb_get_len(name), |
| 127 | val + VMB_LEN, vmb_get_len(val))); |
| 128 | } |
| 129 | |
| 130 | /* add a form file upload */ |
| 131 | void OS_HttpPayload::add(const char *name, const char *filename, |
| 132 | const char *mime_type, CVmDataSource *stream) |
| 133 | { |
| 134 | add(new OS_HttpPayloadItem(name, filename, mime_type, stream)); |
| 135 | } |
| 136 | |
| 137 | /* add a form file upload */ |
| 138 | void OS_HttpPayload::add(const char *name, size_t name_len, |
| 139 | const char *filename, size_t filename_len, |
| 140 | const char *mime_type, size_t mime_type_len, |
| 141 | CVmDataSource *stream) |
| 142 | { |
| 143 | add(new OS_HttpPayloadItem( |
| 144 | name, name_len, filename, filename_len, |
| 145 | mime_type, mime_type_len, stream)); |
| 146 | } |
| 147 | |
| 148 | /* add an item */ |
| 149 | void OS_HttpPayload::add(OS_HttpPayloadItem *item) |
| 150 | { |
| 151 | if (last_item != 0) |
| 152 | last_item->nxt = item; |
| 153 | else |
| 154 | first_item = item; |
| 155 | last_item = item; |
| 156 | } |
| 157 | |
| 158 | /* get the number of items */ |
| 159 | int OS_HttpPayload::count_items() const |
| 160 | { |
| 161 | /* run through the list and count the items */ |
| 162 | int cnt = 0; |
| 163 | for (OS_HttpPayloadItem *cur = first_item ; cur != 0 ; cur = cur->nxt) |
| 164 | ++cnt; |
| 165 | |
| 166 | /* return the number of items */ |
| 167 | return cnt; |
| 168 | } |
| 169 | |
| 170 | /* get the nth item */ |
| 171 | OS_HttpPayloadItem *OS_HttpPayload::get(int n) const |
| 172 | { |
| 173 | /* scan into the list by 'n' items */ |
| 174 | OS_HttpPayloadItem *cur; |
| 175 | for (cur = first_item ; cur != 0 && n != 0 ; cur = cur->nxt, --n) ; |
| 176 | |
| 177 | /* return the item where we stopped */ |
| 178 | return cur; |
| 179 | } |
| 180 | |
| 181 | /* |
| 182 | * Does the payload contain any multipart file data? This returns true if |
| 183 | * any of the fields are uploaded files, false if they're all simple form |
| 184 | * fields. |
| 185 | */ |
| 186 | int OS_HttpPayload::is_multipart() const |
| 187 | { |
| 188 | /* check to see if any of the payload parts are files */ |
| 189 | for (OS_HttpPayloadItem *cur = first_item ; cur != 0 ; cur = cur->nxt) |
| 190 | { |
| 191 | /* if this is a file item, we have a multipart payload */ |
| 192 | if (cur->stream != 0) |
| 193 | return TRUE; |
| 194 | } |
| 195 | |
| 196 | /* we didn't find any file uploads, so it's a simple form */ |
| 197 | return FALSE; |
| 198 | } |
| 199 | |
| 200 | /* |
| 201 | * URL-encode a string into a buffer; returns the required length, not |
| 202 | * counting the terminating null byte. If the buffer is null, we'll simply |
| 203 | * calculate and return the length. |
| 204 | */ |
| 205 | static size_t urlencodestr(char *dst, const char *str) |
| 206 | { |
| 207 | size_t len; |
| 208 | for (len = 0 ; *str != '\0' ; ++str) |
| 209 | { |
| 210 | switch (*str) |
| 211 | { |
| 212 | case '!': |
| 213 | case '*': |
| 214 | case '\'': |
| 215 | case '(': |
| 216 | case ')': |
| 217 | case ';': |
| 218 | case ':': |
| 219 | case '@': |
| 220 | case '&': |
| 221 | case '=': |
| 222 | case '+': |
| 223 | case '$': |
| 224 | case ',': |
| 225 | case '/': |
| 226 | case '?': |
| 227 | case '#': |
| 228 | case '[': |
| 229 | case ']': |
| 230 | /* encode these as % sequences */ |
| 231 | len += 3; |
| 232 | if (dst != 0) |
| 233 | { |
| 234 | sprintf(dst, "%%%02x", (int)*str); |
| 235 | dst += 3; |
| 236 | } |
| 237 | break; |
| 238 | |
| 239 | case ' ': |
| 240 | /* encode space as '+' */ |
| 241 | ++len; |
| 242 | if (dst != 0) |
| 243 | *dst++ = '+'; |
| 244 | break; |
| 245 | |
| 246 | default: |
| 247 | /* encode anything else as-is */ |
| 248 | ++len; |
| 249 | if (dst != 0) |
| 250 | *dst++ = *str; |
| 251 | break; |
| 252 | } |
| 253 | } |
| 254 | |
| 255 | /* add a null terminator (but don't count it in the output length) */ |
| 256 | if (dst != 0) |
| 257 | *dst = '\0'; |
| 258 | |
| 259 | /* return the calculated length */ |
| 260 | return len; |
| 261 | } |
| 262 | |
| 263 | |
| 264 | /* |
| 265 | * Create an application/x-www-form-urlencoded buffer from the payload. |
| 266 | */ |
| 267 | char *OS_HttpPayload::urlencode(size_t &len) const |
| 268 | { |
| 269 | /* |
| 270 | * start by scanning the fields to figure the total buffer length we'll |
| 271 | * need |
| 272 | */ |
| 273 | OS_HttpPayloadItem *item; |
| 274 | for (len = 0, item = first_item ; item != 0 ; item = item->nxt) |
| 275 | { |
| 276 | /* |
| 277 | * if this isn't the first item, we'll need a '&' to separate it |
| 278 | * from the prior item |
| 279 | */ |
| 280 | if (item != first_item) |
| 281 | len += 1; |
| 282 | |
| 283 | /* |
| 284 | * add the item's length: "name=value", with url encoding for both |
| 285 | * strings |
| 286 | */ |
| 287 | len += urlencodestr(0, item->name) + 1 + urlencodestr(0, item->val); |
| 288 | } |
| 289 | |
| 290 | /* allocate the buffer */ |
| 291 | char *putdata = (char *)t3malloc(len + 1); |
| 292 | if (putdata == 0) |
| 293 | return 0; |
| 294 | |
| 295 | /* build the buffer */ |
| 296 | char *p; |
| 297 | for (p = putdata, item = first_item ; item != 0 ; item = item->nxt) |
| 298 | { |
| 299 | /* if this isn't the first item, add the '&' */ |
| 300 | if (item != first_item) |
| 301 | *p++ = '&'; |
| 302 | |
| 303 | /* add "name=urlencodestr(value)" */ |
| 304 | p += urlencodestr(p, item->name); |
| 305 | *p++ = '='; |
| 306 | p += urlencodestr(p, item->val); |
| 307 | } |
| 308 | |
| 309 | /* return the allocated buffer */ |
| 310 | return putdata; |
| 311 | } |