/http_server/stack/message/AWSHttpMessage.cpp
C++ | 97 lines | 70 code | 23 blank | 4 comment | 8 complexity | 54cfd5f6d50bf6d19245930d28fdcf25 MD5 | raw file
1/* Copyright (c) 2009 Yahoo! Inc. All rights reserved. 2 * The copyrights embodied in the content of this file are licensed by Yahoo! Inc. 3 * under the BSD (revised) open source license. 4 */ 5 6#ifndef AWS_HTTP_MESSAGE_H 7#include <AWSHttpMessage.h> 8#endif 9 10#include <string.h> 11#include <stdio.h> 12#include <stdarg.h> 13 14AWSHttpMessage::AWSHttpMessage() : 15 _flags(0x00), 16 _version(110), 17 _headers() 18{ 19} 20 21AWSHttpMessage::~AWSHttpMessage() 22{ 23} 24 25const AWSHttpHeader *AWSHttpMessage::getHeader(unsigned const char *fieldName) const 26{ 27 if (0 == fieldName) 28 { 29 return 0; 30 } 31 32 for (const AWSHttpHeader *header = (const AWSHttpHeader *) _headers.getFirst(); 33 header; 34 header = (const AWSHttpHeader *) header->getNext()) 35 { 36 if (0 == strcasecmp((const char *) fieldName, (const char *) header->getFieldName())) 37 { 38 return header; 39 } 40 } 41 42 return 0; 43} 44 45 46ESFError AWSHttpMessage::addHeader(unsigned const char *fieldName, 47 unsigned const char *fieldValue, 48 ESFAllocator *allocator) 49{ 50 if (! fieldName || ! allocator) 51 { 52 return ESF_NULL_POINTER; 53 } 54 55 int fieldNameLength = strlen((const char *) fieldName); 56 int fieldValueLength = strlen((const char *) fieldValue); 57 58 unsigned char *block = (unsigned char *) allocator->allocate(sizeof(AWSHttpHeader) + 59 fieldNameLength + 60 fieldValueLength + 2); 61 62 if (! block) 63 { 64 return ESF_OUT_OF_MEMORY; 65 } 66 67 memcpy(block + sizeof(AWSHttpHeader), fieldName, fieldNameLength); 68 block[sizeof(AWSHttpHeader) + fieldNameLength] = 0; 69 70 memcpy(block + sizeof(AWSHttpHeader) + fieldNameLength + 1, fieldValue, fieldValueLength); 71 block[sizeof(AWSHttpHeader) + fieldNameLength + fieldValueLength + 1] = 0; 72 73 AWSHttpHeader *header = new(block) AWSHttpHeader(block + sizeof(AWSHttpHeader), 74 block + sizeof(AWSHttpHeader) + fieldNameLength + 1); 75 76 _headers.addLast(header); 77 78 return ESF_SUCCESS; 79} 80 81ESFError AWSHttpMessage::addHeader(ESFAllocator *allocator, 82 unsigned const char *fieldName, 83 unsigned const char *fieldValueFormat, 84 ...) 85{ 86 char buffer[1024]; 87 88 va_list vaList; 89 90 va_start(vaList, (const char *) fieldValueFormat); 91 92 vsnprintf(buffer, sizeof(buffer), (const char *) fieldValueFormat, vaList); 93 94 va_end( vaList ); 95 96 return addHeader(fieldName, (unsigned const char *) buffer, allocator); 97}