atscppapi  1.0.9
C++ wrapper for Apache Traffic Server API
 All Classes Files Functions Enumerations Enumerator Macros
Headers.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2013 LinkedIn Corp. All rights reserved.
3  * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file
4  * except in compliance with the License. You may obtain a copy of the license at
5  * http://www.apache.org/licenses/LICENSE-2.0
6  *
7  * Unless required by applicable law or agreed to in writing, software distributed under the
8  * License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
9  * either express or implied.
10  *
11  */
12 
13 /**
14  * @file Headers.h
15  * @author Brian Geffon
16  * @author Manjesh Nilange
17  */
18 
19 #pragma once
20 #ifndef ATSCPPAPI_HEADERS_H_
21 #define ATSCPPAPI_HEADERS_H_
22 
23 #include <map>
24 #include <list>
26 #include <atscppapi/noncopyable.h>
27 
28 namespace atscppapi {
29 
30 struct HeadersState;
31 class Request;
32 class ClientRequest;
33 class Response;
34 
35 /**
36  * @brief Encapsulates the headers portion of a request or response.
37  */
38 class Headers: noncopyable {
39 public:
40 
41  enum Type { TYPE_REQUEST, TYPE_RESPONSE };
42 
43  Headers(Type type = TYPE_REQUEST);
44 
45  Type getType() const;
46 
47  typedef std::map<std::string, std::list<std::string>, CaseInsensitiveStringComparator> NameValuesMap;
48 
49  typedef NameValuesMap::size_type size_type;
50  typedef NameValuesMap::const_iterator const_iterator;
51  typedef NameValuesMap::const_reverse_iterator const_reverse_iterator;
52 
53  /**
54  * @return Iterator to first header.
55  */
56  const_iterator begin() const;
57 
58  /**
59  * @return Iterator to end of headers.
60  */
61  const_iterator end() const;
62 
63  /**
64  * @return Iterator to last header.
65  */
66  const_reverse_iterator rbegin() const;
67 
68  /**
69  * @return Iterator to "reverse end"
70  */
71  const_reverse_iterator rend() const;
72 
73  /**
74  * @param key Name of header
75  * @return Iterator to header if exists, else end()
76  */
77  const_iterator find(const std::string &key) const;
78 
79  /**
80  * @param key Name of header
81  * @return 1 if header exists, 0 if not.
82  */
83  size_type count(const std::string &key) const;
84 
85  /**
86  * Erases header with given name.
87  *
88  * @param key Name of header
89  * @return 1 if header was erased, 0 if not.
90  */
91  size_type erase(const std::string &key);
92 
93  /**
94  * Sets the given header and values. If a header of same name existed, that is
95  * deleted. Else header is appended.
96  *
97  * @param pair Contains the name and list of values.
98  *
99  * @return Iterator to header set.
100  */
101  const_iterator set(const std::pair<std::string, std::list<std::string> > &pair);
102 
103  /**
104  * Sets the given header and values. If a header of same name existed, that is
105  * deleted. Else header is appended.
106  *
107  * @param key Name of header
108  * @param val List of values
109  *
110  * @return Iterator to header set.
111  */
112  const_iterator set(const std::string &key, const std::list<std::string> &val);
113 
114  /**
115  * Sets the given header and values. If a header of same name existed, that is
116  * deleted. Else header is appended.
117  *
118  * @param key Name of header
119  * @param val Value
120  *
121  * @return Iterator to header set.
122  */
123  const_iterator set(const std::string &key, const std::string &val);
124 
125  /**
126  * Appends a new header. If a header of the same name exists, value(s) is tacked
127  * on that the end of current value(s).
128  *
129  * @param pair Contains the name and list of values.
130  *
131  * @return Iterator to header appended.
132  */
133  const_iterator append(const std::pair<std::string, std::list<std::string> > &pair);
134 
135  /**
136  * Appends a new header. If a header of the same name exists, value(s) is tacked
137  * on that the end of current value(s).
138  *
139  * @param key Name of header
140  * @param val List of values
141  *
142  * @return Iterator to header appended.
143  */
144  const_iterator append(const std::string &key, const std::list<std::string> &val);
145 
146  /**
147  * Appends a new header. If a header of the same name exists, value(s) is tacked
148  * on that the end of current value(s).
149  *
150  * @param key Name of header
151  * @param val Value
152  *
153  * @return Iterator to header appended.
154  */
155  const_iterator append(const std::string &key, const std::string &val);
156 
157  /**
158  * Joins provided list of values with delimiter (defaulting to ',').
159  *
160  * @return Composite string
161  */
162  static std::string getJoinedValues(const std::list<std::string> &values, char delimiter = ',');
163 
164  /**
165  * Joins values of provided header with delimiter (defaulting to ',').
166  *
167  * @return Composite string if header exists, else empty strings.
168  */
169  std::string getJoinedValues(const std::string &key, char value_delimiter = ',');
170 
171  /**
172  * @return True if there are no headers.
173  */
174  bool empty() const;
175 
176  /**
177  * @return Largest possible size of underlying map.
178  */
179  size_type max_size() const;
180 
181  /**
182  * @return Number of headers currently stored.
183  */
184  size_type size() const;
185 
186  typedef std::map<std::string, std::list<std::string> > RequestCookieMap;
187 
188  /**
189  * @return Cookies in the "Cookie" headers of a request object.
190  */
191  const RequestCookieMap &getRequestCookies() const;
192 
193  struct ResponseCookie {
194  std::string name_;
195  std::string value_;
196  std::string comment_;
197  std::string domain_;
198  int max_age_;
199  std::string path_;
200  bool secure_;
201  int version_;
202  ResponseCookie() : max_age_(0), secure_(false), version_(0) { };
203  };
204 
205  /**
206  * @return Cookies in the "Set-Cookie" headers of a response object.
207  */
208  const std::list<ResponseCookie> &getResponseCookies() const;
209 
210  /** Adds a request cookie */
211  bool addCookie(const std::string &name, const std::string &value);
212 
213  /** Adds a response cookie */
214  bool addCookie(const ResponseCookie &response_cookie);
215 
216  /** Sets, i.e., clears current value and adds new value, of a request cookie */
217  bool setCookie(const std::string &name, const std::string &value);
218 
219  /** Sets, i.e., clears current value and adds new value, of a response cookie */
220  bool setCookie(const ResponseCookie &response_cookie);
221 
222  /** Deletes a cookie */
223  bool deleteCookie(const std::string &name);
224 
225  ~Headers();
226 private:
227  HeadersState *state_;
228  bool checkAndInitHeaders() const;
229  void init(void *hdr_buf, void *hdr_loc);
230  void initDetached();
231  void setType(Type type);
232  void updateRequestCookieHeaderFromMap();
233  const_iterator doBasicAppend(const std::pair<std::string, std::list<std::string> > &pair);
234  size_type doBasicErase(const std::string &key);
235  friend class Request;
236  friend class ClientRequest;
237  friend class Response;
238 };
239 
240 }
241 
242 #endif