00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025 #ifndef _PASSENGER_MESSAGE_READERS_WRITERS_H_
00026 #define _PASSENGER_MESSAGE_READERS_WRITERS_H_
00027
00028 #include <boost/cstdint.hpp>
00029 #include <algorithm>
00030 #include <vector>
00031 #include <string>
00032 #include <sys/types.h>
00033 #include <cstring>
00034 #include <arpa/inet.h>
00035 #include "StaticString.h"
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050
00051
00052
00053
00054
00055
00056
00057
00058
00059
00060
00061
00062
00063
00064
00065
00066
00067
00068
00069
00070
00071
00072
00073
00074
00075
00076
00077
00078
00079
00080
00081
00082
00083
00084
00085
00086
00087
00088
00089
00090
00091
00092
00093
00094
00095
00096
00097
00098
00099
00100
00101
00102
00103
00104
00105
00106
00107
00108
00109
00110 namespace Passenger {
00111
00112 using namespace std;
00113
00114
00115
00116
00117 class Uint16Reader {
00118 private:
00119 uint16_t val;
00120 uint8_t consumed;
00121
00122 public:
00123 Uint16Reader() {
00124 consumed = 0;
00125 }
00126
00127 void reset() {
00128 consumed = 0;
00129 }
00130
00131 size_t feed(const char *data, size_t size) {
00132 size_t locallyConsumed;
00133
00134 locallyConsumed = std::min(size, sizeof(uint16_t) - consumed);
00135 memcpy(&val + consumed, data, locallyConsumed);
00136 consumed += locallyConsumed;
00137 if (done()) {
00138 val = ntohs(val);
00139 }
00140 return locallyConsumed;
00141 }
00142
00143 bool done() const {
00144 return consumed == sizeof(uint16_t);
00145 }
00146
00147 uint16_t value() const {
00148 return val;
00149 }
00150
00151 void generate(void *buf, uint16_t val) const {
00152 val = htons(val);
00153 memcpy(buf, &val, sizeof(val));
00154 }
00155 };
00156
00157
00158
00159
00160 class Uint32Reader {
00161 private:
00162 uint32_t val;
00163 uint8_t consumed;
00164
00165 public:
00166 Uint32Reader() {
00167 consumed = 0;
00168 }
00169
00170 void reset() {
00171 consumed = 0;
00172 }
00173
00174 size_t feed(const char *data, size_t size) {
00175 size_t locallyConsumed;
00176
00177 locallyConsumed = std::min(size, sizeof(uint32_t) - consumed);
00178 memcpy(&val + consumed, data, locallyConsumed);
00179 consumed += locallyConsumed;
00180 if (done()) {
00181 val = ntohl(val);
00182 }
00183 return locallyConsumed;
00184 }
00185
00186 bool done() const {
00187 return consumed == sizeof(uint32_t);
00188 }
00189
00190 uint32_t value() const {
00191 return val;
00192 }
00193 };
00194
00195
00196
00197
00198 class ArrayReader {
00199 public:
00200 enum Error {
00201 TOO_LARGE
00202 };
00203
00204 private:
00205 enum State {
00206 READING_HEADER,
00207 READING_BODY,
00208 DONE,
00209 ERROR
00210 };
00211
00212 uint16_t toReserve;
00213 uint16_t maxSize;
00214 Uint16Reader headerReader;
00215 uint8_t state;
00216 uint8_t error;
00217 string buffer;
00218 vector<StaticString> result;
00219
00220 void parseBody(const char *data, size_t size) {
00221 const char *start = data;
00222 const char *terminator;
00223 size_t rest = size;
00224
00225 while ((terminator = (const char *) memchr(start, '\0', rest)) != NULL) {
00226 size_t len = terminator - start;
00227 result.push_back(StaticString(start, len));
00228 start = terminator + 1;
00229 rest = size - (start - data);
00230 }
00231 }
00232
00233 public:
00234 ArrayReader() {
00235 state = READING_HEADER;
00236 toReserve = 0;
00237 maxSize = 0;
00238 }
00239
00240 void reserve(uint16_t size) {
00241 toReserve = size;
00242 result.reserve(size);
00243 }
00244
00245 void setMaxSize(uint16_t size) {
00246 maxSize = size;
00247 }
00248
00249 void reset() {
00250 state = READING_HEADER;
00251 headerReader.reset();
00252 buffer.clear();
00253 result.clear();
00254 if (toReserve > 0) {
00255 result.reserve(toReserve);
00256 }
00257 }
00258
00259 size_t feed(const char *data, size_t size) {
00260 size_t consumed = 0;
00261
00262 while (consumed < size && !done()) {
00263 const char *current = data + consumed;
00264 size_t rest = size - consumed;
00265
00266 switch (state) {
00267 case READING_HEADER:
00268 consumed += headerReader.feed(current, rest);
00269 if (headerReader.done()) {
00270 if (maxSize > 0 && headerReader.value() > maxSize) {
00271 state = ERROR;
00272 error = TOO_LARGE;
00273 } else {
00274 state = READING_BODY;
00275 }
00276 }
00277 break;
00278 case READING_BODY:
00279 if (buffer.empty() && rest >= headerReader.value()) {
00280 parseBody(current, headerReader.value());
00281 state = DONE;
00282 consumed += headerReader.value();
00283 } else {
00284 size_t toConsume = std::min(rest,
00285 headerReader.value() - buffer.size());
00286 if (buffer.capacity() < headerReader.value()) {
00287 buffer.reserve(headerReader.value());
00288 }
00289 buffer.append(current, toConsume);
00290 consumed += toConsume;
00291 if (buffer.size() == headerReader.value()) {
00292 parseBody(buffer.data(), buffer.size());
00293 state = DONE;
00294 }
00295 }
00296 break;
00297 default:
00298
00299 abort();
00300 }
00301 }
00302 return consumed;
00303 }
00304
00305 bool done() const {
00306 return state == DONE || state == ERROR;
00307 }
00308
00309 bool hasError() const {
00310 return state == ERROR;
00311 }
00312
00313 Error errorCode() const {
00314 return (Error) error;
00315 }
00316
00317 const vector<StaticString> &value() const {
00318 return result;
00319 }
00320 };
00321
00322
00323
00324
00325 class ScalarReader {
00326 public:
00327 enum Error {
00328 TOO_LARGE
00329 };
00330
00331 private:
00332 enum State {
00333 READING_HEADER,
00334 READING_BODY,
00335 DONE,
00336 ERROR
00337 };
00338
00339 uint8_t state;
00340 uint8_t error;
00341 uint32_t maxSize;
00342 Uint32Reader headerReader;
00343 string buffer;
00344 StaticString result;
00345
00346 public:
00347 ScalarReader(uint32_t maxSize = 0) {
00348 state = READING_HEADER;
00349 this->maxSize = maxSize;
00350 }
00351
00352 void reset() {
00353 state = READING_HEADER;
00354 headerReader.reset();
00355 buffer.clear();
00356 }
00357
00358 size_t feed(const char *data, size_t size) {
00359 size_t consumed = 0;
00360
00361 while (consumed < size && !done()) {
00362 const char *current = data + consumed;
00363 size_t rest = size - consumed;
00364
00365 switch (state) {
00366 case READING_HEADER:
00367 consumed += headerReader.feed(current, rest);
00368 if (headerReader.done()) {
00369 if (maxSize > 0 && headerReader.value() > maxSize) {
00370 state = ERROR;
00371 error = TOO_LARGE;
00372 } else {
00373 state = READING_BODY;
00374 }
00375 }
00376 break;
00377 case READING_BODY:
00378 if (buffer.empty() && rest >= headerReader.value()) {
00379 result = StaticString(current, headerReader.value());
00380 state = DONE;
00381 consumed += headerReader.value();
00382 } else {
00383 size_t toConsume = std::min(rest,
00384 headerReader.value() - buffer.size());
00385 if (buffer.capacity() < headerReader.value()) {
00386 buffer.reserve(headerReader.value());
00387 }
00388 buffer.append(current, toConsume);
00389 consumed += toConsume;
00390 if (buffer.size() == headerReader.value()) {
00391 result = StaticString(buffer);
00392 state = DONE;
00393 }
00394 }
00395 break;
00396 default:
00397
00398 abort();
00399 };
00400 }
00401 return consumed;
00402 }
00403
00404 bool done() const {
00405 return state == DONE || state == ERROR;
00406 }
00407
00408 bool hasError() const {
00409 return state == ERROR;
00410 }
00411
00412 Error errorCode() const {
00413 return (Error) error;
00414 }
00415
00416 StaticString value() const {
00417 return result;
00418 }
00419 };
00420
00421 }
00422
00423 #endif