mbed.org implementation of the abstract SmartREST library for the Cumulocity Platform SmartREST protocol.

Dependents:   MbedSmartRestMain MbedSmartRestMain

Committer:
Cumulocity
Date:
Sat Nov 15 12:21:41 2014 +0100
Revision:
13:aba98ad2ac1b
Parent:
0:099f76422485
Updated from revision 0b898f0efc6d

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Cumulocity 0:099f76422485 1 /*
Cumulocity 0:099f76422485 2 * HTTPResponseFilter.cpp
Cumulocity 0:099f76422485 3 *
Cumulocity 0:099f76422485 4 * Created on: Feb 1, 2013
Cumulocity 0:099f76422485 5 * * Authors: Vincent Wochnik <v.wochnik@gmail.com>
Cumulocity 0:099f76422485 6 *
Cumulocity 0:099f76422485 7 * Copyright (c) 2013 Cumulocity GmbH
Cumulocity 0:099f76422485 8 *
Cumulocity 0:099f76422485 9 * Permission is hereby granted, free of charge, to any person obtaining
Cumulocity 0:099f76422485 10 * a copy of this software and associated documentation files (the
Cumulocity 0:099f76422485 11 * "Software"), to deal in the Software without restriction, including
Cumulocity 0:099f76422485 12 * without limitation the rights to use, copy, modify, merge, publish,
Cumulocity 0:099f76422485 13 * distribute, sublicense, and/or sell copies of the Software, and to
Cumulocity 0:099f76422485 14 * permit persons to whom the Software is furnished to do so, subject to
Cumulocity 0:099f76422485 15 * the following conditions:
Cumulocity 0:099f76422485 16 *
Cumulocity 0:099f76422485 17 * The above copyright notice and this permission notice shall be
Cumulocity 0:099f76422485 18 * included in all copies or substantial portions of the Software.
Cumulocity 0:099f76422485 19 *
Cumulocity 0:099f76422485 20 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
Cumulocity 0:099f76422485 21 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
Cumulocity 0:099f76422485 22 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
Cumulocity 0:099f76422485 23 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
Cumulocity 0:099f76422485 24 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
Cumulocity 0:099f76422485 25 * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
Cumulocity 0:099f76422485 26 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
Cumulocity 0:099f76422485 27 */
Cumulocity 0:099f76422485 28
Cumulocity 0:099f76422485 29 #include "HTTPResponseFilter.h"
Cumulocity 0:099f76422485 30 #include <stdlib.h>
Cumulocity 0:099f76422485 31 #include <string.h>
Cumulocity 0:099f76422485 32 #include <ctype.h>
Cumulocity 0:099f76422485 33 #include <stdio.h>
Cumulocity 0:099f76422485 34
Cumulocity 0:099f76422485 35 const char *cExpectedStatus = "HTTP/1.* ";
Cumulocity 0:099f76422485 36 const char *cContentLength = "Content-Length: ";
Cumulocity 0:099f76422485 37
Cumulocity 0:099f76422485 38 HTTPResponseFilter::HTTPResponseFilter(AbstractDataSource& source) : _source(source), _state(RESPF_STATE_INIT)
Cumulocity 0:099f76422485 39 {
Cumulocity 0:099f76422485 40 _length = _read = 0;
Cumulocity 0:099f76422485 41 }
Cumulocity 0:099f76422485 42
Cumulocity 0:099f76422485 43 HTTPResponseFilter::~HTTPResponseFilter()
Cumulocity 0:099f76422485 44 {
Cumulocity 0:099f76422485 45 }
Cumulocity 0:099f76422485 46
Cumulocity 0:099f76422485 47 char HTTPResponseFilter::read()
Cumulocity 0:099f76422485 48 {
Cumulocity 0:099f76422485 49 if (_state != RESPF_STATE_READ_HEADERS)
Cumulocity 0:099f76422485 50 return 0;
Cumulocity 0:099f76422485 51 if ((_length > 0) && (_length == _read))
Cumulocity 0:099f76422485 52 return 0;
Cumulocity 0:099f76422485 53 _read++;
Cumulocity 0:099f76422485 54 return _source.read();
Cumulocity 0:099f76422485 55 }
Cumulocity 0:099f76422485 56
Cumulocity 0:099f76422485 57 uint8_t HTTPResponseFilter::status()
Cumulocity 0:099f76422485 58 {
Cumulocity 0:099f76422485 59 if (_state != RESPF_STATE_READ_HEADERS)
Cumulocity 0:099f76422485 60 return DS_STATUS_ERROR;
Cumulocity 0:099f76422485 61 if ((_length > 0) && (_length == _read))
Cumulocity 0:099f76422485 62 return DS_STATUS_CLOSED;
Cumulocity 0:099f76422485 63 return _source.status();
Cumulocity 0:099f76422485 64 }
Cumulocity 0:099f76422485 65
Cumulocity 0:099f76422485 66 uint16_t HTTPResponseFilter::readStatus()
Cumulocity 0:099f76422485 67 {
Cumulocity 0:099f76422485 68 uint16_t res = 0;
Cumulocity 0:099f76422485 69 uint8_t state = 0;
Cumulocity 0:099f76422485 70 char c;
Cumulocity 0:099f76422485 71 size_t offset = 0;
Cumulocity 0:099f76422485 72 uint8_t status = DS_STATUS_OK;
Cumulocity 0:099f76422485 73
Cumulocity 0:099f76422485 74 if (_state != RESPF_STATE_INIT)
Cumulocity 0:099f76422485 75 return 0;
Cumulocity 0:099f76422485 76
Cumulocity 0:099f76422485 77 while ((state < 3) && (((c = _source.read()) > 0) || ((status = _source.status()) == DS_STATUS_OK))) {
Cumulocity 0:099f76422485 78 switch (state) {
Cumulocity 0:099f76422485 79 case 0: // read expected status line
Cumulocity 0:099f76422485 80 if ((cExpectedStatus[offset] != c) && (cExpectedStatus[offset] != '*'))
Cumulocity 0:099f76422485 81 state = 3;
Cumulocity 0:099f76422485 82 offset++;
Cumulocity 0:099f76422485 83 if (offset == strlen(cExpectedStatus)) {
Cumulocity 0:099f76422485 84 state = 1;
Cumulocity 0:099f76422485 85 }
Cumulocity 0:099f76422485 86 break;
Cumulocity 0:099f76422485 87 case 1:
Cumulocity 0:099f76422485 88 if (isspace(c))
Cumulocity 0:099f76422485 89 state = 2;
Cumulocity 0:099f76422485 90 if (isdigit(c))
Cumulocity 0:099f76422485 91 res = (res * 10) + (c - '0');
Cumulocity 0:099f76422485 92 break;
Cumulocity 0:099f76422485 93 case 2:
Cumulocity 0:099f76422485 94 if (c == '\n')
Cumulocity 0:099f76422485 95 state = 3;
Cumulocity 0:099f76422485 96 break;
Cumulocity 0:099f76422485 97 }
Cumulocity 0:099f76422485 98 }
Cumulocity 0:099f76422485 99
Cumulocity 0:099f76422485 100 if ((status != DS_STATUS_OK) || (state != 3))
Cumulocity 0:099f76422485 101 return 0;
Cumulocity 0:099f76422485 102
Cumulocity 0:099f76422485 103 _state = RESPF_STATE_READ_STATUS;
Cumulocity 0:099f76422485 104 return res;
Cumulocity 0:099f76422485 105 }
Cumulocity 0:099f76422485 106
Cumulocity 0:099f76422485 107 bool HTTPResponseFilter::skipHeaders()
Cumulocity 0:099f76422485 108 {
Cumulocity 0:099f76422485 109 uint16_t res = 0;
Cumulocity 0:099f76422485 110 uint8_t state = 0;
Cumulocity 0:099f76422485 111 char c;
Cumulocity 0:099f76422485 112 size_t offset = 0;
Cumulocity 0:099f76422485 113 uint8_t status = DS_STATUS_OK;
Cumulocity 0:099f76422485 114
Cumulocity 0:099f76422485 115 if (_state != RESPF_STATE_READ_STATUS)
Cumulocity 0:099f76422485 116 return false;
Cumulocity 0:099f76422485 117
Cumulocity 0:099f76422485 118 while ((state < 5) && (((c = _source.read()) > 0) || ((status = _source.status()) == DS_STATUS_OK))) {
Cumulocity 0:099f76422485 119 switch (state) {
Cumulocity 0:099f76422485 120 case 0: // start of line
Cumulocity 0:099f76422485 121 if (offset == 0) {
Cumulocity 0:099f76422485 122 if (cContentLength[0] == c)
Cumulocity 0:099f76422485 123 state = 1;
Cumulocity 0:099f76422485 124 if (c == '\r')
Cumulocity 0:099f76422485 125 state = 4;
Cumulocity 0:099f76422485 126 } else {
Cumulocity 0:099f76422485 127 if (c == '\r')
Cumulocity 0:099f76422485 128 state = 3;
Cumulocity 0:099f76422485 129 }
Cumulocity 0:099f76422485 130 offset++;
Cumulocity 0:099f76422485 131 break;
Cumulocity 0:099f76422485 132 case 1:
Cumulocity 0:099f76422485 133 if (c == '\r')
Cumulocity 0:099f76422485 134 state = 3;
Cumulocity 0:099f76422485 135 else if (cContentLength[offset] != c)
Cumulocity 0:099f76422485 136 state = 0;
Cumulocity 0:099f76422485 137 else if (offset == strlen(cContentLength)-1)
Cumulocity 0:099f76422485 138 state = 2;
Cumulocity 0:099f76422485 139 offset++;
Cumulocity 0:099f76422485 140 break;
Cumulocity 0:099f76422485 141 case 2:
Cumulocity 0:099f76422485 142 if (isdigit(c))
Cumulocity 0:099f76422485 143 _length = (_length * 10) + (c - '0');
Cumulocity 0:099f76422485 144 else if (c == '\r')
Cumulocity 0:099f76422485 145 state = 3;
Cumulocity 0:099f76422485 146 else
Cumulocity 0:099f76422485 147 state = 0;
Cumulocity 0:099f76422485 148 offset++;
Cumulocity 0:099f76422485 149 break;
Cumulocity 0:099f76422485 150 case 3:
Cumulocity 0:099f76422485 151 if (c == '\n') {
Cumulocity 0:099f76422485 152 state = 0;
Cumulocity 0:099f76422485 153 offset = 0;
Cumulocity 0:099f76422485 154 }
Cumulocity 0:099f76422485 155 break;
Cumulocity 0:099f76422485 156 case 4:
Cumulocity 0:099f76422485 157 if (c == '\n')
Cumulocity 0:099f76422485 158 state = 5;
Cumulocity 0:099f76422485 159 break;
Cumulocity 0:099f76422485 160 }
Cumulocity 0:099f76422485 161 }
Cumulocity 0:099f76422485 162
Cumulocity 0:099f76422485 163 if ((status != DS_STATUS_OK) || (state != 5))
Cumulocity 0:099f76422485 164 return false;
Cumulocity 0:099f76422485 165
Cumulocity 0:099f76422485 166 _state = RESPF_STATE_READ_HEADERS;
Cumulocity 0:099f76422485 167 return true;
Cumulocity 0:099f76422485 168 }
Cumulocity 0:099f76422485 169
Cumulocity 0:099f76422485 170 void HTTPResponseFilter::reset()
Cumulocity 0:099f76422485 171 {
Cumulocity 0:099f76422485 172 _state = RESPF_STATE_INIT;
Cumulocity 0:099f76422485 173 _length = _read = 0;
Cumulocity 0:099f76422485 174 }