1 /*
2   Licensed to the Apache Software Foundation (ASF) under one
3   or more contributor license agreements.  See the NOTICE file
4   distributed with this work for additional information
5   regarding copyright ownership.  The ASF licenses this file
6   to you under the Apache License, Version 2.0 (the
7   "License"); you may not use this file except in compliance
8   with the License.  You may obtain a copy of the License at
9 
10   http://www.apache.org/licenses/LICENSE-2.0
11 
12   Unless required by applicable law or agreed to in writing, software
13   distributed under the License is distributed on an "AS IS" BASIS,
14   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   See the License for the specific language governing permissions and
16   limitations under the License.
17 */
18 
19 #pragma once
20 
21 #include <string>
22 #include <unordered_map>
23 
24 using std::string;
25 using std::unordered_map;
26 
27 class CookieJar
28 {
29 public:
CookieJar()30   CookieJar(){};
~CookieJar()31   ~CookieJar(){};
32 
33   bool create(const string &strCookie);
34 
35   CookieJar(const CookieJar &) = delete;
36   CookieJar &operator=(const CookieJar &) = delete;
37 
38   bool get_full(const string &cookie_name, string &val);
39   bool get_part(const string &cookie_name, const string &part_name, string &val);
40 
41 private:
42   int verify_name(char *name);
43   int verify_value(char *val, int val_len);
44   int parse(const string &arg, const char *sepstr, bool val_check, bool mainElement);
45 
46   void addElement(const char *key, const char *val);
47   void addSubElement(const char *key, const char *val);
48 
49   class CookieVal
50   {
51   public:
52     unordered_map<string, string> m_subelements;
53     string m_val;
54     bool parts_inited = false;
55   };
56   CookieVal *m_currentVal = nullptr;
57 
58   unordered_map<string, CookieVal> m_jar;
59 };
60