HttpHeaders.h
Go to the documentation of this file.
1 /*--------------------------------------------------------------------------------------+
2 |
3 | Supplied under applicable software license agreement.
4 |
5 | Copyright (c) 2018 Bentley Systems, Incorporated. All rights reserved.
6 |
7 +---------------------------------------------------------------------------------------*/
8 #pragma once
9 
10 #include <Bentley/WString.h>
11 #include <Bentley/bmap.h>
12 #include <Bentley/DateTime.h>
13 #include <BeHttp/Http.h>
14 
15 #define REQUESTHEADER_ContentType_ApplicationJson "application/json"
16 #define REQUESTHEADER_ContentType_ApplicationXml "application/xml"
17 #define REQUESTHEADER_ContentType_TextHtml "text/html"
18 
20 
21 //----------------------------------------------------------------------------------------
22 // @bsimethod Bentley Systems
23 //----------------------------------------------------------------------------------------
25  {
26  bool operator()(Utf8String const& lhs, Utf8String const& rhs) const { return (lhs.CompareToI(rhs) < 0); }
27  };
28 
29 // RFC 7230 states: "Each header field consists of a name followed by a colon (":") and the field value. Field names are case-insensitive.
30 typedef bmap<Utf8String, Utf8String, CompareInsensitiveUtfString> HttpHeaderMap;
31 
34 
35 /*--------------------------------------------------------------------------------------+
36 * @bsiclass Bentley Systems
37 +---------------+---------------+---------------+---------------+---------------+------*/
39 {
40 protected:
42 
43 public:
44  // Return reference to internal map
45  HttpHeaderMap const& GetMap() const {return m_headers;}
46 
47  // Set header field value. Pass empty to remove previous value
48  BEHTTP_EXPORT void SetValue(Utf8StringCR field, Utf8StringCR value);
49 
50  // Set header field value or comma-append it to existing value
51  // Multiple header values: http://www.w3.org/Protocols/rfc2616/rfc2616-sec4.html#sec4.2
52  BEHTTP_EXPORT void AddValue(Utf8StringCR field, Utf8StringCR value);
53 
54  // Return header field value
55  BEHTTP_EXPORT Utf8CP GetValue(Utf8StringCR field) const;
56 
57  // Clear all headers fields
58  void Clear() {return m_headers.clear();}
59 };
60 
61 /*--------------------------------------------------------------------------------------+
62 * @bsiclass Bentley Systems
63 +---------------+---------------+---------------+---------------+---------------+------*/
65 {
66  // Accept header
67  void SetAccept(Utf8StringCR value) {SetValue("Accept", value);}
68  Utf8CP GetAccept() const {return GetValue("Accept");}
69 
70  // Accept-Language header
71  void SetAcceptLanguage(Utf8StringCR langugageRanges) {SetValue("Accept-Language", langugageRanges);}
72  Utf8CP GetAcceptLanguage() const {return GetValue("Accept-Language");}
73 
74  // Authorization header
75  void SetAuthorization(Utf8StringCR authorization) {SetValue("Authorization", authorization);}
76  Utf8CP GetAuthorization() const {return GetValue("Authorization");}
77 
78  // Content-Disposition header
79  void SetContentDisposition(Utf8StringCR value) {SetValue("Content-Disposition", value);}
80  Utf8CP GetContentDisposition() const {return GetValue("Content-Disposition");}
81 
82  // Content-Range header
83  void SetContentRange(Utf8StringCR range) {SetValue("Content-Range", range);}
84  Utf8CP GetContentRange() const {return GetValue("Content-Range");}
85 
86  // Content-Type header
87  void SetContentType(Utf8StringCR type) {SetValue("Content-Type", type);}
88  Utf8CP GetContentType() const {return GetValue("Content-Type");}
89 
90  // User-Agent header to identify application in the server. Example - "Bentley.SampleNavigator/2.0"
91  void SetUserAgent(Utf8StringCR userAgent) {SetValue("User-Agent", userAgent);}
92  Utf8CP GetUserAgent() const {return GetValue("User-Agent");}
93 
94  // If-Modified-Since header
95  void SetIfModifiedSince(Utf8StringCR dateTime) {SetValue("If-Modified-Since", dateTime);}
97  Utf8CP GetIfModifiedSince() const {return GetValue("If-Modified-Since");}
98 
99  // If-None-Match header
100  void SetIfNoneMatch(Utf8StringCR etag) {SetValue("If-None-Match", etag);}
101  Utf8CP GetIfNoneMatch() const {return GetValue("If-None-Match");}
102 
103  // If-Match header
104  void SetIfMatch(Utf8StringCR etag) {SetValue("If-Match", etag);}
105  Utf8CP GetIfMatch() const {return GetValue("If-Match");}
106 };
107 
108 /*--------------------------------------------------------------------------------------+
109 * @bsiclass Bentley Systems
110 +---------------+---------------+---------------+---------------+---------------+------*/
112 {
113  // ETag header
114  void SetETag(Utf8StringCR etag) {SetValue("ETag", etag);}
115  Utf8CP GetETag() const {return GetValue("ETag");}
116 
117  // Range header
118  void SetRange(Utf8StringCR range) {SetValue("Range", range);}
119  Utf8CP GetRange() const {return GetValue("Range");}
120 
121  // Content-Range header
122  void SetContentRange(Utf8StringCR range) {SetValue("Content-Range", range);}
123  Utf8CP GetContentRange() const {return GetValue("Content-Range");}
124 
125  // Content-Type header
126  void SetContentType(Utf8StringCR type) {SetValue("Content-Type", type);}
127  Utf8CP GetContentType() const {return GetValue("Content-Type");}
128 
129  // Location header
130  void SetLocation(Utf8StringCR type) {SetValue("Location", type);}
131  Utf8CP GetLocation() const {return GetValue("Location");}
132 
133  // Server header
134  void SetServer(Utf8StringCR server) {SetValue("Server", server);}
135  Utf8CP GetServer() const {return GetValue("Server");}
136 
137  // Cache-Control header
138  void SetCacheControl(Utf8StringCR cacheControl) {SetValue("Cache-Control", cacheControl);}
139  Utf8CP GetCacheControl() const {return GetValue("Cache-Control");}
140 };
141 
142 /*--------------------------------------------------------------------------------------+
143 * @bsiclass Bentley Systems
144 +---------------+---------------+---------------+---------------+---------------+------*/
146 {
147 private:
148  uint64_t m_from = 0;
149  uint64_t m_to = 0;
150 
151 public:
153  RangeHeaderValue(uint64_t from, uint64_t to) : m_from(from), m_to(to) {}
154 
155  BEHTTP_EXPORT static BentleyStatus Parse(Utf8CP stringValue, RangeHeaderValue& valueOut);
156  BEHTTP_EXPORT Utf8String ToString() const;
157  uint64_t GetFrom() const {return m_from;}
158  uint64_t GetTo() const {return m_to;}
159 };
160 
161 /*--------------------------------------------------------------------------------------+
162 * @bsiclass Bentley Systems
163 +---------------+---------------+---------------+---------------+---------------+------*/
165 {
166 private:
167  bool m_hasRange = false;
168  bool m_hasLength = false;
169  uint64_t m_from = 0;
170  uint64_t m_to = 0;
171  uint64_t m_length = 0;
172 
173 public:
175  explicit ContentRangeHeaderValue(uint64_t length) : m_length(length) {}
176  ContentRangeHeaderValue(uint64_t from, uint64_t to) : m_hasRange(true), m_hasLength(false), m_from(from), m_to(to), m_length(0){}
177  ContentRangeHeaderValue(uint64_t from, uint64_t to, uint64_t length) : m_hasRange(true), m_hasLength(true), m_from(from), m_to(to), m_length(length){}
178 
179  BEHTTP_EXPORT static BentleyStatus Parse(Utf8CP stringValue, ContentRangeHeaderValue& valueOut);
180  BEHTTP_EXPORT Utf8String ToString() const;
181 
182  bool HasRange() const {return m_hasRange;}
183  bool HasLength() const {return m_hasLength;}
184  uint64_t GetFrom() const {return m_from;}
185  uint64_t GetTo() const {return m_to;}
186  uint64_t GetLength() const {return m_length;}
187 };
188 
Utf8CP GetUserAgent() const
Definition: HttpHeaders.h:92
uint64_t GetFrom() const
Definition: HttpHeaders.h:157
#define BEGIN_BENTLEY_HTTP_NAMESPACE
Definition: Http.h:17
Provides Bentley specific date/time functions (Bentley/BeAssert.h).
Utf8CP GetIfModifiedSince() const
Definition: HttpHeaders.h:97
void SetLocation(Utf8StringCR type)
Definition: HttpHeaders.h:130
Utf8CP GetContentDisposition() const
Definition: HttpHeaders.h:80
void Clear()
Definition: HttpHeaders.h:58
Utf8CP GetAuthorization() const
Definition: HttpHeaders.h:76
uint64_t GetTo() const
Definition: HttpHeaders.h:185
HttpHeaderMap m_headers
Definition: HttpHeaders.h:41
Definition: HttpHeaders.h:24
ContentRangeHeaderValue()
Definition: HttpHeaders.h:174
void SetAuthorization(Utf8StringCR authorization)
Definition: HttpHeaders.h:75
Definition: HttpHeaders.h:145
void SetServer(Utf8StringCR server)
Definition: HttpHeaders.h:134
void SetAcceptLanguage(Utf8StringCR langugageRanges)
Definition: HttpHeaders.h:71
DateTime const & DateTimeCR
Definition: DateTime.h:443
struct Bentley::Utf8String const & Utf8StringCR
Definition: Bentley.h:241
Utf8CP GetRange() const
Definition: HttpHeaders.h:119
ContentRangeHeaderValue(uint64_t length)
Definition: HttpHeaders.h:175
Utf8CP GetServer() const
Definition: HttpHeaders.h:135
void SetAccept(Utf8StringCR value)
Definition: HttpHeaders.h:67
#define BEHTTP_EXPORT
Definition: Http.h:14
RangeHeaderValue(uint64_t from, uint64_t to)
Definition: HttpHeaders.h:153
Definition: HttpHeaders.h:38
void SetContentType(Utf8StringCR type)
Definition: HttpHeaders.h:126
static BEHTTP_EXPORT BentleyStatus Parse(Utf8CP stringValue, ContentRangeHeaderValue &valueOut)
Utf8CP GetIfMatch() const
Definition: HttpHeaders.h:105
Utf8CP GetContentType() const
Definition: HttpHeaders.h:127
RangeHeaderValue()
Definition: HttpHeaders.h:152
void SetContentRange(Utf8StringCR range)
Definition: HttpHeaders.h:83
bool HasLength() const
Definition: HttpHeaders.h:183
unsigned long long uint64_t
Definition: Bentley.r.h:95
uint64_t GetLength() const
Definition: HttpHeaders.h:186
BentleyStatus
Definition: Bentley.h:208
bool HasRange() const
Definition: HttpHeaders.h:182
Utf8CP GetLocation() const
Definition: HttpHeaders.h:131
Utf8CP GetIfNoneMatch() const
Definition: HttpHeaders.h:101
void SetETag(Utf8StringCR etag)
Definition: HttpHeaders.h:114
void SetIfMatch(Utf8StringCR etag)
Definition: HttpHeaders.h:104
static BEHTTP_EXPORT BentleyStatus Parse(Utf8CP stringValue, RangeHeaderValue &valueOut)
ContentRangeHeaderValue(uint64_t from, uint64_t to)
Definition: HttpHeaders.h:176
Definition: HttpHeaders.h:64
ContentRangeHeaderValue(uint64_t from, uint64_t to, uint64_t length)
Definition: HttpHeaders.h:177
void SetContentRange(Utf8StringCR range)
Definition: HttpHeaders.h:122
void SetIfModifiedSince(Utf8StringCR dateTime)
Definition: HttpHeaders.h:95
void SetRange(Utf8StringCR range)
Definition: HttpHeaders.h:118
Definition: HttpHeaders.h:111
HttpHeaderMap const & GetMap() const
Definition: HttpHeaders.h:45
Utf8CP GetContentRange() const
Definition: HttpHeaders.h:84
Utf8Char const * Utf8CP
Definition: Bentley.h:229
void SetCacheControl(Utf8StringCR cacheControl)
Definition: HttpHeaders.h:138
void SetIfNoneMatch(Utf8StringCR etag)
Definition: HttpHeaders.h:100
uint64_t GetFrom() const
Definition: HttpHeaders.h:184
#define END_BENTLEY_HTTP_NAMESPACE
Definition: Http.h:18
Definition: HttpHeaders.h:164
Utf8CP GetContentType() const
Definition: HttpHeaders.h:88
void SetContentDisposition(Utf8StringCR value)
Definition: HttpHeaders.h:79
BEHTTP_EXPORT Utf8String ToString() const
bmap< Utf8String, Utf8String, CompareInsensitiveUtfString > HttpHeaderMap
Definition: HttpHeaders.h:30
#define DEFINE_POINTER_SUFFIX_TYPEDEFS(_structname_)
Definition: Bentley.h:136
Utf8CP GetETag() const
Definition: HttpHeaders.h:115
Utf8CP GetAcceptLanguage() const
Definition: HttpHeaders.h:72
uint64_t GetTo() const
Definition: HttpHeaders.h:158
void SetContentType(Utf8StringCR type)
Definition: HttpHeaders.h:87
BEHTTP_EXPORT Utf8CP GetValue(Utf8StringCR field) const
BEHTTP_EXPORT Utf8String ToString() const
BEHTTP_EXPORT void SetValue(Utf8StringCR field, Utf8StringCR value)
Utf8CP GetAccept() const
Definition: HttpHeaders.h:68
void SetUserAgent(Utf8StringCR userAgent)
Definition: HttpHeaders.h:91
Utf8CP GetCacheControl() const
Definition: HttpHeaders.h:139
bool operator()(Utf8String const &lhs, Utf8String const &rhs) const
Definition: HttpHeaders.h:26
Utf8CP GetContentRange() const
Definition: HttpHeaders.h:123

Copyright © 2017 Bentley Systems, Incorporated. All rights reserved.