David Fletcher
/
CC3000WebServer
A Port of TI's Webserver for the CC3000
WebServer/HttpString.cpp@2:e6a185df9e4c, 2013-09-16 (annotated)
- Committer:
- dflet
- Date:
- Mon Sep 16 18:37:14 2013 +0000
- Revision:
- 2:e6a185df9e4c
- Parent:
- 0:6ad60d78b315
ADC and Leds now work on board and config.html page.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
dflet | 0:6ad60d78b315 | 1 | /***************************************************************************** |
dflet | 0:6ad60d78b315 | 2 | * |
dflet | 0:6ad60d78b315 | 3 | * HttpString.c |
dflet | 0:6ad60d78b315 | 4 | * Copyright (C) 2011 Texas Instruments Incorporated - http://www.ti.com/ |
dflet | 0:6ad60d78b315 | 5 | * |
dflet | 0:6ad60d78b315 | 6 | * Redistribution and use in source and binary forms, with or without |
dflet | 0:6ad60d78b315 | 7 | * modification, are permitted provided that the following conditions |
dflet | 0:6ad60d78b315 | 8 | * are met: |
dflet | 0:6ad60d78b315 | 9 | * |
dflet | 0:6ad60d78b315 | 10 | * Redistributions of source code must retain the above copyright |
dflet | 0:6ad60d78b315 | 11 | * notice, this list of conditions and the following disclaimer. |
dflet | 0:6ad60d78b315 | 12 | * |
dflet | 0:6ad60d78b315 | 13 | * Redistributions in binary form must reproduce the above copyright |
dflet | 0:6ad60d78b315 | 14 | * notice, this list of conditions and the following disclaimer in the |
dflet | 0:6ad60d78b315 | 15 | * documentation and/or other materials provided with the |
dflet | 0:6ad60d78b315 | 16 | * distribution. |
dflet | 0:6ad60d78b315 | 17 | * |
dflet | 0:6ad60d78b315 | 18 | * Neither the name of Texas Instruments Incorporated nor the names of |
dflet | 0:6ad60d78b315 | 19 | * its contributors may be used to endorse or promote products derived |
dflet | 0:6ad60d78b315 | 20 | * from this software without specific prior written permission. |
dflet | 0:6ad60d78b315 | 21 | * |
dflet | 0:6ad60d78b315 | 22 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
dflet | 0:6ad60d78b315 | 23 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
dflet | 0:6ad60d78b315 | 24 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
dflet | 0:6ad60d78b315 | 25 | * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
dflet | 0:6ad60d78b315 | 26 | * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
dflet | 0:6ad60d78b315 | 27 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
dflet | 0:6ad60d78b315 | 28 | * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
dflet | 0:6ad60d78b315 | 29 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
dflet | 0:6ad60d78b315 | 30 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
dflet | 0:6ad60d78b315 | 31 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
dflet | 0:6ad60d78b315 | 32 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
dflet | 0:6ad60d78b315 | 33 | * |
dflet | 0:6ad60d78b315 | 34 | *****************************************************************************/ |
dflet | 0:6ad60d78b315 | 35 | #include "mbed.h" |
dflet | 0:6ad60d78b315 | 36 | #include "string.h" |
dflet | 0:6ad60d78b315 | 37 | #include <stdlib.h> |
dflet | 0:6ad60d78b315 | 38 | #include "HttpString.h" |
dflet | 0:6ad60d78b315 | 39 | |
dflet | 0:6ad60d78b315 | 40 | /** |
dflet | 0:6ad60d78b315 | 41 | * @addtogroup HttpString |
dflet | 0:6ad60d78b315 | 42 | * @{ |
dflet | 0:6ad60d78b315 | 43 | */ |
dflet | 0:6ad60d78b315 | 44 | |
dflet | 0:6ad60d78b315 | 45 | char digits[] = "0123456789"; |
dflet | 0:6ad60d78b315 | 46 | char hexDigits[] = "0123456789abcdef"; |
dflet | 0:6ad60d78b315 | 47 | |
dflet | 0:6ad60d78b315 | 48 | int HttpString_strcmp(struct HttpBlob first, struct HttpBlob second) |
dflet | 0:6ad60d78b315 | 49 | { |
dflet | 0:6ad60d78b315 | 50 | int min,res; |
dflet | 0:6ad60d78b315 | 51 | if (first.uLength > second.uLength) |
dflet | 0:6ad60d78b315 | 52 | min = second.uLength; |
dflet | 0:6ad60d78b315 | 53 | else |
dflet | 0:6ad60d78b315 | 54 | min = first.uLength; |
dflet | 0:6ad60d78b315 | 55 | |
dflet | 0:6ad60d78b315 | 56 | // Compare a common length which might be equal |
dflet | 0:6ad60d78b315 | 57 | res = memcmp(first.pData, second.pData, min); |
dflet | 0:6ad60d78b315 | 58 | if (res != 0) |
dflet | 0:6ad60d78b315 | 59 | return res; |
dflet | 0:6ad60d78b315 | 60 | |
dflet | 0:6ad60d78b315 | 61 | // Common length is equal, so the longer blob is "larger" |
dflet | 0:6ad60d78b315 | 62 | if (first.uLength > second.uLength) |
dflet | 0:6ad60d78b315 | 63 | return 1; |
dflet | 0:6ad60d78b315 | 64 | else if (first.uLength < second.uLength) |
dflet | 0:6ad60d78b315 | 65 | return -1; |
dflet | 0:6ad60d78b315 | 66 | else |
dflet | 0:6ad60d78b315 | 67 | return 0; |
dflet | 0:6ad60d78b315 | 68 | } |
dflet | 0:6ad60d78b315 | 69 | |
dflet | 0:6ad60d78b315 | 70 | static void ToLower(char * str, uint16 len) |
dflet | 0:6ad60d78b315 | 71 | { |
dflet | 0:6ad60d78b315 | 72 | int i; |
dflet | 0:6ad60d78b315 | 73 | for (i=0 ; i<=len; i++) |
dflet | 0:6ad60d78b315 | 74 | { |
dflet | 0:6ad60d78b315 | 75 | if (str[i]>='A' && str[i]<='Z') |
dflet | 0:6ad60d78b315 | 76 | str[i] = str[i] + 32; |
dflet | 0:6ad60d78b315 | 77 | } |
dflet | 0:6ad60d78b315 | 78 | } |
dflet | 0:6ad60d78b315 | 79 | |
dflet | 0:6ad60d78b315 | 80 | uint8* HttpString_nextToken(char* pToken, uint16 uTokenLength, struct HttpBlob blob) |
dflet | 0:6ad60d78b315 | 81 | { |
dflet | 0:6ad60d78b315 | 82 | uint8* pch = blob.pData; |
dflet | 0:6ad60d78b315 | 83 | struct HttpBlob partialBlob; |
dflet | 0:6ad60d78b315 | 84 | struct HttpBlob token; |
dflet | 0:6ad60d78b315 | 85 | token.pData = (uint8*)pToken; |
dflet | 0:6ad60d78b315 | 86 | token.uLength = uTokenLength; |
dflet | 0:6ad60d78b315 | 87 | ToLower((char *)blob.pData, blob.uLength); |
dflet | 0:6ad60d78b315 | 88 | while (pch < blob.pData + blob.uLength) |
dflet | 0:6ad60d78b315 | 89 | { |
dflet | 0:6ad60d78b315 | 90 | // Calculate how many blob bytes we should search |
dflet | 0:6ad60d78b315 | 91 | int nMaxCount = blob.uLength - (pch - blob.pData) - uTokenLength + 1; |
dflet | 0:6ad60d78b315 | 92 | if (nMaxCount < 1) |
dflet | 0:6ad60d78b315 | 93 | return NULL; |
dflet | 0:6ad60d78b315 | 94 | |
dflet | 0:6ad60d78b315 | 95 | // Search for the first character of the token |
dflet | 0:6ad60d78b315 | 96 | pch = (uint8*)memchr(pch, pToken[0], nMaxCount); |
dflet | 0:6ad60d78b315 | 97 | if (pch==NULL) |
dflet | 0:6ad60d78b315 | 98 | return NULL; |
dflet | 0:6ad60d78b315 | 99 | |
dflet | 0:6ad60d78b315 | 100 | // Found first character, now compare the rest |
dflet | 0:6ad60d78b315 | 101 | partialBlob.pData = pch; |
dflet | 0:6ad60d78b315 | 102 | partialBlob.uLength = uTokenLength; |
dflet | 0:6ad60d78b315 | 103 | if (HttpString_strcmp(token, partialBlob)==0) |
dflet | 0:6ad60d78b315 | 104 | return pch; |
dflet | 0:6ad60d78b315 | 105 | |
dflet | 0:6ad60d78b315 | 106 | // Skip this byte, and look for the token in the rest of the blob |
dflet | 0:6ad60d78b315 | 107 | ++pch; |
dflet | 0:6ad60d78b315 | 108 | } |
dflet | 0:6ad60d78b315 | 109 | return NULL; |
dflet | 0:6ad60d78b315 | 110 | } |
dflet | 0:6ad60d78b315 | 111 | |
dflet | 0:6ad60d78b315 | 112 | uint32 HttpString_atou(struct HttpBlob string) |
dflet | 0:6ad60d78b315 | 113 | { |
dflet | 0:6ad60d78b315 | 114 | return atoi((const char*)string.pData); |
dflet | 0:6ad60d78b315 | 115 | } |
dflet | 0:6ad60d78b315 | 116 | |
dflet | 0:6ad60d78b315 | 117 | void HttpString_utoa(uint32 uNum, struct HttpBlob* pString) |
dflet | 0:6ad60d78b315 | 118 | { |
dflet | 0:6ad60d78b315 | 119 | char* ptr; |
dflet | 0:6ad60d78b315 | 120 | uint32 uTemp = uNum; |
dflet | 0:6ad60d78b315 | 121 | |
dflet | 0:6ad60d78b315 | 122 | // value 0 is a special format |
dflet | 0:6ad60d78b315 | 123 | if (uNum == 0) |
dflet | 0:6ad60d78b315 | 124 | { |
dflet | 0:6ad60d78b315 | 125 | pString->uLength = 1; |
dflet | 0:6ad60d78b315 | 126 | *pString->pData = '0'; |
dflet | 0:6ad60d78b315 | 127 | return; |
dflet | 0:6ad60d78b315 | 128 | } |
dflet | 0:6ad60d78b315 | 129 | |
dflet | 0:6ad60d78b315 | 130 | // Find out the length of the number, in decimal base |
dflet | 0:6ad60d78b315 | 131 | pString->uLength = 0; |
dflet | 0:6ad60d78b315 | 132 | while (uTemp > 0) |
dflet | 0:6ad60d78b315 | 133 | { |
dflet | 0:6ad60d78b315 | 134 | uTemp /= 10; |
dflet | 0:6ad60d78b315 | 135 | pString->uLength++; |
dflet | 0:6ad60d78b315 | 136 | } |
dflet | 0:6ad60d78b315 | 137 | |
dflet | 0:6ad60d78b315 | 138 | // Do the actual formatting, right to left |
dflet | 0:6ad60d78b315 | 139 | uTemp = uNum; |
dflet | 0:6ad60d78b315 | 140 | ptr = (char*)pString->pData + pString->uLength; |
dflet | 0:6ad60d78b315 | 141 | while (uTemp > 0) |
dflet | 0:6ad60d78b315 | 142 | { |
dflet | 0:6ad60d78b315 | 143 | --ptr; |
dflet | 0:6ad60d78b315 | 144 | *ptr = digits[uTemp % 10]; |
dflet | 0:6ad60d78b315 | 145 | uTemp /= 10; |
dflet | 0:6ad60d78b315 | 146 | } |
dflet | 0:6ad60d78b315 | 147 | } |
dflet | 0:6ad60d78b315 | 148 | |
dflet | 0:6ad60d78b315 | 149 | void HttpString_htoa(uint32 uNum, struct HttpBlob* pString, uint8 bPadZero) |
dflet | 0:6ad60d78b315 | 150 | { |
dflet | 0:6ad60d78b315 | 151 | uint8* ptr; |
dflet | 0:6ad60d78b315 | 152 | uint32 uTemp = uNum; |
dflet | 0:6ad60d78b315 | 153 | |
dflet | 0:6ad60d78b315 | 154 | if (!bPadZero) |
dflet | 0:6ad60d78b315 | 155 | { |
dflet | 0:6ad60d78b315 | 156 | // value 0 is a special format |
dflet | 0:6ad60d78b315 | 157 | if (uNum == 0) |
dflet | 0:6ad60d78b315 | 158 | { |
dflet | 0:6ad60d78b315 | 159 | pString->uLength = 1; |
dflet | 0:6ad60d78b315 | 160 | *pString->pData = '0'; |
dflet | 0:6ad60d78b315 | 161 | return; |
dflet | 0:6ad60d78b315 | 162 | } |
dflet | 0:6ad60d78b315 | 163 | |
dflet | 0:6ad60d78b315 | 164 | // Find out the length of the number, in hexadecimal base |
dflet | 0:6ad60d78b315 | 165 | pString->uLength = 0; |
dflet | 0:6ad60d78b315 | 166 | while (uTemp > 0) |
dflet | 0:6ad60d78b315 | 167 | { |
dflet | 0:6ad60d78b315 | 168 | uTemp /= 16; |
dflet | 0:6ad60d78b315 | 169 | pString->uLength++; |
dflet | 0:6ad60d78b315 | 170 | } |
dflet | 0:6ad60d78b315 | 171 | } |
dflet | 0:6ad60d78b315 | 172 | |
dflet | 0:6ad60d78b315 | 173 | // Do the actual formatting, right to left |
dflet | 0:6ad60d78b315 | 174 | uTemp = uNum; |
dflet | 0:6ad60d78b315 | 175 | ptr = pString->pData + pString->uLength; |
dflet | 0:6ad60d78b315 | 176 | while (ptr > pString->pData) |
dflet | 0:6ad60d78b315 | 177 | { |
dflet | 0:6ad60d78b315 | 178 | --ptr; |
dflet | 0:6ad60d78b315 | 179 | *ptr = (uint8)hexDigits[uTemp % 16]; |
dflet | 0:6ad60d78b315 | 180 | uTemp /= 16; |
dflet | 0:6ad60d78b315 | 181 | } |
dflet | 0:6ad60d78b315 | 182 | } |
dflet | 0:6ad60d78b315 | 183 |