![]() |
RTBKit
0.9
Open-source framework to create real-time ad bidding systems.
|
00001 /* http_header.h -*- C++ -*- 00002 Jeremy Barnes, 18 February 2011 00003 Copyright (c) 2011 Datacratic. All rights reserved. 00004 00005 http header parsing class. 00006 */ 00007 00008 #pragma once 00009 00010 #include <string> 00011 #include <map> 00012 #include <iostream> 00013 #include <vector> 00014 #include "jml/arch/exception.h" 00015 00016 00017 namespace Datacratic { 00018 00019 /*****************************************************************************/ 00020 /* REST PARAMS */ 00021 /*****************************************************************************/ 00022 00023 struct RestParams 00024 : public std::vector<std::pair<std::string, std::string> > { 00025 RestParams() 00026 { 00027 } 00028 00029 RestParams(std::initializer_list<std::pair<std::string, std::string> > l) 00030 : std::vector<std::pair<std::string, std::string> >(l.begin(), l.end()) 00031 { 00032 } 00033 00034 bool hasValue(const std::string & key) const; 00035 00039 std::string getValue(const std::string & key) const; 00040 00041 std::string uriEscaped() const; 00042 00043 static RestParams fromBinary(const std::string & binary); 00044 std::string toBinary() const; 00045 }; 00046 00047 00048 /*****************************************************************************/ 00049 /* HTTP HEADER */ 00050 /*****************************************************************************/ 00051 00054 struct HttpHeader { 00055 HttpHeader() 00056 : contentLength(-1), isChunked(false) 00057 { 00058 } 00059 00060 void swap(HttpHeader & other); 00061 00062 void parse(const std::string & headerAndData); 00063 00064 std::string verb; // GET, PUT, etc 00065 std::string resource; // after the get 00066 std::string version; // after the get 00067 00068 RestParams queryParams; // Query parameters pulled out of the URL 00069 00070 // These headers are automatically pulled out 00071 std::string contentType; 00072 ssize_t contentLength; 00073 bool isChunked; 00074 00075 // The rest of the headers are here 00076 std::map<std::string, std::string> headers; 00077 00078 std::string getHeader(const std::string & key) const 00079 { 00080 auto it = headers.find(key); 00081 if (it == headers.end()) 00082 throw ML::Exception("couldn't find header " + key); 00083 return it->second; 00084 } 00085 00086 std::string tryGetHeader(const std::string & key) const 00087 { 00088 auto it = headers.find(key); 00089 if (it == headers.end()) 00090 return ""; 00091 return it->second; 00092 } 00093 00094 // If some portion of the data is known, it's put in here 00095 std::string knownData; 00096 }; 00097 00098 std::ostream & operator << (std::ostream & stream, const HttpHeader & header); 00099 00103 std::string getResponseReasonPhrase(int code); 00104 00105 } // namespace Datacratic