Panda3D
httpEntityTag.cxx
Go to the documentation of this file.
1 /**
2  * PANDA 3D SOFTWARE
3  * Copyright (c) Carnegie Mellon University. All rights reserved.
4  *
5  * All use of this software is subject to the terms of the revised BSD
6  * license. You should have received a copy of this license along
7  * with this source code in a file named "LICENSE."
8  *
9  * @file httpEntityTag.cxx
10  * @author drose
11  * @date 2003-01-28
12  */
13 
14 #include "httpEntityTag.h"
15 
16 using std::string;
17 
18 
19 /**
20  * This constructor accepts a string as formatted from an HTTP server (e.g.
21  * the tag is quoted, with an optional W/ prefix.)
22  */
23 HTTPEntityTag::
24 HTTPEntityTag(const string &text) {
25  _weak = false;
26 
27  size_t p = 0;
28  if (text.length() >= 2) {
29  string sub = text.substr(0, 2);
30  if (sub == "W/" || sub == "w/") {
31  _weak = true;
32  p = 2;
33  }
34  }
35 
36  // Unquote the string.
37  bool quoted = false;
38  if (p < text.length() && text[p] == '"') {
39  quoted = true;
40  p++;
41  }
42  while (p < text.length() && !(quoted && text[p] == '"')) {
43  if (text[p] == '\\') {
44  p++;
45  }
46  _tag += text[p];
47  p++;
48  }
49 }
50 
51 /**
52  * Returns the entity tag formatted for sending to an HTTP server (the tag is
53  * quoted, with a conditional W prefix).
54  */
55 string HTTPEntityTag::
56 get_string() const {
57  std::ostringstream result;
58  if (_weak) {
59  result << "W/";
60  }
61  result << '"';
62 
63  for (string::const_iterator ti = _tag.begin(); ti != _tag.end(); ++ti) {
64  switch (*ti) {
65  case '"':
66  case '\\':
67  result << '\\';
68  // fall through
69 
70  default:
71  result << (*ti);
72  }
73  }
74 
75  result << '"';
76 
77  return result.str();
78 }
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
std::string get_string() const
Returns the entity tag formatted for sending to an HTTP server (the tag is quoted,...