Own fork of MbedSmartRest
Dependents: MbedSmartRestMain MbedSmartRestMain
Fork of MbedSmartRest by
HTTPResponseFilter.cpp@26:9c36af176d91, 2015-08-10 (annotated)
- Committer:
- xinlei
- Date:
- Mon Aug 10 10:39:53 2015 +0000
- Revision:
- 26:9c36af176d91
- Parent:
- 19:81dfc04ce0bb
removed traffic accounting
Who changed what in which revision?
User | Revision | Line number | New 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; |
xinlei | 18:16192696c106 | 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 | uint8_t state = 0; |
Cumulocity | 0:099f76422485 | 110 | char c; |
Cumulocity | 0:099f76422485 | 111 | size_t offset = 0; |
Cumulocity | 0:099f76422485 | 112 | uint8_t status = DS_STATUS_OK; |
Cumulocity | 0:099f76422485 | 113 | |
Cumulocity | 0:099f76422485 | 114 | if (_state != RESPF_STATE_READ_STATUS) |
Cumulocity | 0:099f76422485 | 115 | return false; |
Cumulocity | 0:099f76422485 | 116 | |
Cumulocity | 0:099f76422485 | 117 | while ((state < 5) && (((c = _source.read()) > 0) || ((status = _source.status()) == DS_STATUS_OK))) { |
Cumulocity | 0:099f76422485 | 118 | switch (state) { |
Cumulocity | 0:099f76422485 | 119 | case 0: // start of line |
Cumulocity | 0:099f76422485 | 120 | if (offset == 0) { |
Cumulocity | 0:099f76422485 | 121 | if (cContentLength[0] == c) |
Cumulocity | 0:099f76422485 | 122 | state = 1; |
Cumulocity | 0:099f76422485 | 123 | if (c == '\r') |
Cumulocity | 0:099f76422485 | 124 | state = 4; |
Cumulocity | 0:099f76422485 | 125 | } else { |
Cumulocity | 0:099f76422485 | 126 | if (c == '\r') |
Cumulocity | 0:099f76422485 | 127 | state = 3; |
Cumulocity | 0:099f76422485 | 128 | } |
Cumulocity | 0:099f76422485 | 129 | offset++; |
Cumulocity | 0:099f76422485 | 130 | break; |
Cumulocity | 0:099f76422485 | 131 | case 1: |
Cumulocity | 0:099f76422485 | 132 | if (c == '\r') |
Cumulocity | 0:099f76422485 | 133 | state = 3; |
Cumulocity | 0:099f76422485 | 134 | else if (cContentLength[offset] != c) |
Cumulocity | 0:099f76422485 | 135 | state = 0; |
Cumulocity | 0:099f76422485 | 136 | else if (offset == strlen(cContentLength)-1) |
Cumulocity | 0:099f76422485 | 137 | state = 2; |
Cumulocity | 0:099f76422485 | 138 | offset++; |
Cumulocity | 0:099f76422485 | 139 | break; |
Cumulocity | 0:099f76422485 | 140 | case 2: |
Cumulocity | 0:099f76422485 | 141 | if (isdigit(c)) |
Cumulocity | 0:099f76422485 | 142 | _length = (_length * 10) + (c - '0'); |
Cumulocity | 0:099f76422485 | 143 | else if (c == '\r') |
Cumulocity | 0:099f76422485 | 144 | state = 3; |
Cumulocity | 0:099f76422485 | 145 | else |
Cumulocity | 0:099f76422485 | 146 | state = 0; |
Cumulocity | 0:099f76422485 | 147 | offset++; |
Cumulocity | 0:099f76422485 | 148 | break; |
Cumulocity | 0:099f76422485 | 149 | case 3: |
Cumulocity | 0:099f76422485 | 150 | if (c == '\n') { |
Cumulocity | 0:099f76422485 | 151 | state = 0; |
Cumulocity | 0:099f76422485 | 152 | offset = 0; |
Cumulocity | 0:099f76422485 | 153 | } |
Cumulocity | 0:099f76422485 | 154 | break; |
Cumulocity | 0:099f76422485 | 155 | case 4: |
Cumulocity | 0:099f76422485 | 156 | if (c == '\n') |
Cumulocity | 0:099f76422485 | 157 | state = 5; |
Cumulocity | 0:099f76422485 | 158 | break; |
Cumulocity | 0:099f76422485 | 159 | } |
Cumulocity | 0:099f76422485 | 160 | } |
Cumulocity | 0:099f76422485 | 161 | |
Cumulocity | 0:099f76422485 | 162 | if ((status != DS_STATUS_OK) || (state != 5)) |
Cumulocity | 0:099f76422485 | 163 | return false; |
Cumulocity | 0:099f76422485 | 164 | |
Cumulocity | 0:099f76422485 | 165 | _state = RESPF_STATE_READ_HEADERS; |
Cumulocity | 0:099f76422485 | 166 | return true; |
Cumulocity | 0:099f76422485 | 167 | } |
Cumulocity | 0:099f76422485 | 168 | |
Cumulocity | 0:099f76422485 | 169 | void HTTPResponseFilter::reset() |
Cumulocity | 0:099f76422485 | 170 | { |
Cumulocity | 0:099f76422485 | 171 | _state = RESPF_STATE_INIT; |
Cumulocity | 0:099f76422485 | 172 | _length = _read = 0; |
Cumulocity | 0:099f76422485 | 173 | } |