A simple web service over HTTP library. Calls a HTTP server via GET, and returns the response wrapped in a XML parser. All calls are synchronous. Needs the NetServicesMin, DNSResolver, TcpLineStream and spxml libraries. The code for URL handling has been copied directly from the original NetServices library (Thanks to Donatien!).

Committer:
hlipka
Date:
Fri Feb 18 13:15:50 2011 +0000
Revision:
2:687430e7f63a
Parent:
0:5e8527b638e1
added license

Who changed what in which revision?

UserRevisionLine numberNew contents of line
hlipka 0:5e8527b638e1 1
hlipka 0:5e8527b638e1 2 /*
hlipka 0:5e8527b638e1 3 Copyright (c) 2010 Donatien Garnier (donatiengar [at] gmail [dot] com)
hlipka 0:5e8527b638e1 4
hlipka 0:5e8527b638e1 5 Permission is hereby granted, free of charge, to any person obtaining a copy
hlipka 0:5e8527b638e1 6 of this software and associated documentation files (the "Software"), to deal
hlipka 0:5e8527b638e1 7 in the Software without restriction, including without limitation the rights
hlipka 0:5e8527b638e1 8 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
hlipka 0:5e8527b638e1 9 copies of the Software, and to permit persons to whom the Software is
hlipka 0:5e8527b638e1 10 furnished to do so, subject to the following conditions:
hlipka 0:5e8527b638e1 11
hlipka 0:5e8527b638e1 12 The above copyright notice and this permission notice shall be included in
hlipka 0:5e8527b638e1 13 all copies or substantial portions of the Software.
hlipka 0:5e8527b638e1 14
hlipka 0:5e8527b638e1 15 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
hlipka 0:5e8527b638e1 16 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
hlipka 0:5e8527b638e1 17 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
hlipka 0:5e8527b638e1 18 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
hlipka 0:5e8527b638e1 19 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
hlipka 0:5e8527b638e1 20 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
hlipka 0:5e8527b638e1 21 THE SOFTWARE.
hlipka 0:5e8527b638e1 22 */
hlipka 0:5e8527b638e1 23
hlipka 0:5e8527b638e1 24 #include "url.h"
hlipka 0:5e8527b638e1 25 #include <stdlib.h>
hlipka 0:5e8527b638e1 26 #include <ctype.h>
hlipka 0:5e8527b638e1 27
hlipka 0:5e8527b638e1 28 //From http://www.geekhideout.com/urlcode.shtml
hlipka 0:5e8527b638e1 29
hlipka 0:5e8527b638e1 30 char from_hex(char ch);
hlipka 0:5e8527b638e1 31 char to_hex(char code);
hlipka 0:5e8527b638e1 32
hlipka 0:5e8527b638e1 33 /* Converts a hex character to its integer value */
hlipka 0:5e8527b638e1 34 char from_hex(char ch) {
hlipka 0:5e8527b638e1 35 return isdigit(ch) ? ch - '0' : tolower(ch) - 'a' + 10;
hlipka 0:5e8527b638e1 36 }
hlipka 0:5e8527b638e1 37
hlipka 0:5e8527b638e1 38 /* Converts an integer value to its hex character*/
hlipka 0:5e8527b638e1 39 char to_hex(char code) {
hlipka 0:5e8527b638e1 40 static char hex[] = "0123456789abcdef";
hlipka 0:5e8527b638e1 41 return hex[code & 15];
hlipka 0:5e8527b638e1 42 }
hlipka 0:5e8527b638e1 43
hlipka 0:5e8527b638e1 44 /* Returns a url-encoded version of str */
hlipka 0:5e8527b638e1 45 /* IMPORTANT: be sure to free() the returned string after use */
hlipka 0:5e8527b638e1 46 char *url_encode(char *str) {
hlipka 0:5e8527b638e1 47 char *pstr = str, *buf = (char*)malloc(strlen(str) * 3 + 1), *pbuf = buf;
hlipka 0:5e8527b638e1 48 while (*pstr) {
hlipka 0:5e8527b638e1 49 if (isalnum(*pstr) || *pstr == '-' || *pstr == '_' || *pstr == '.' || *pstr == '~')
hlipka 0:5e8527b638e1 50 *pbuf++ = *pstr;
hlipka 0:5e8527b638e1 51 else if (*pstr == ' ')
hlipka 0:5e8527b638e1 52 *pbuf++ = '+';
hlipka 0:5e8527b638e1 53 else
hlipka 0:5e8527b638e1 54 *pbuf++ = '%', *pbuf++ = to_hex(*pstr >> 4), *pbuf++ = to_hex(*pstr & 15);
hlipka 0:5e8527b638e1 55 pstr++;
hlipka 0:5e8527b638e1 56 }
hlipka 0:5e8527b638e1 57 *pbuf = '\0';
hlipka 0:5e8527b638e1 58 return buf;
hlipka 0:5e8527b638e1 59 }
hlipka 0:5e8527b638e1 60
hlipka 0:5e8527b638e1 61 /* Returns a url-decoded version of str */
hlipka 0:5e8527b638e1 62 /* IMPORTANT: be sure to free() the returned string after use */
hlipka 0:5e8527b638e1 63 char *url_decode(char *str) {
hlipka 0:5e8527b638e1 64 char *pstr = str, *buf = (char*)malloc(strlen(str) + 1), *pbuf = buf;
hlipka 0:5e8527b638e1 65 while (*pstr) {
hlipka 0:5e8527b638e1 66 if (*pstr == '%') {
hlipka 0:5e8527b638e1 67 if (pstr[1] && pstr[2]) {
hlipka 0:5e8527b638e1 68 *pbuf++ = from_hex(pstr[1]) << 4 | from_hex(pstr[2]);
hlipka 0:5e8527b638e1 69 pstr += 2;
hlipka 0:5e8527b638e1 70 }
hlipka 0:5e8527b638e1 71 } else if (*pstr == '+') {
hlipka 0:5e8527b638e1 72 *pbuf++ = ' ';
hlipka 0:5e8527b638e1 73 } else {
hlipka 0:5e8527b638e1 74 *pbuf++ = *pstr;
hlipka 0:5e8527b638e1 75 }
hlipka 0:5e8527b638e1 76 pstr++;
hlipka 0:5e8527b638e1 77 }
hlipka 0:5e8527b638e1 78 *pbuf = '\0';
hlipka 0:5e8527b638e1 79 return buf;
hlipka 0:5e8527b638e1 80 }