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