Panda3D
 All Classes Functions Variables Enumerations
httpEntityTag.cxx
00001 // Filename: httpEntityTag.cxx
00002 // Created by:  drose (28Jan03)
00003 //
00004 ////////////////////////////////////////////////////////////////////
00005 //
00006 // PANDA 3D SOFTWARE
00007 // Copyright (c) Carnegie Mellon University.  All rights reserved.
00008 //
00009 // All use of this software is subject to the terms of the revised BSD
00010 // license.  You should have received a copy of this license along
00011 // with this source code in a file named "LICENSE."
00012 //
00013 ////////////////////////////////////////////////////////////////////
00014 
00015 #include "httpEntityTag.h"
00016 
00017 
00018 ////////////////////////////////////////////////////////////////////
00019 //     Function: HTTPEntityTag::Constructor
00020 //       Access: Published
00021 //  Description: This constructor accepts a string as formatted from
00022 //               an HTTP server (e.g. the tag is quoted, with an
00023 //               optional W/ prefix.)
00024 ////////////////////////////////////////////////////////////////////
00025 HTTPEntityTag::
00026 HTTPEntityTag(const string &text) {
00027   _weak = false;
00028 
00029   size_t p = 0;
00030   if (text.length() >= 2) {
00031     string sub = text.substr(0, 2);
00032     if (sub == "W/" || sub == "w/") {
00033       _weak = true;
00034       p = 2;
00035     }
00036   }
00037 
00038   // Unquote the string.
00039   bool quoted = false;
00040   if (p < text.length() && text[p] == '"') {
00041     quoted = true;
00042     p++;
00043   }
00044   while (p < text.length() && !(quoted && text[p] == '"')) {
00045     if (text[p] == '\\') {
00046       p++;
00047     }
00048     _tag += text[p];
00049     p++;
00050   }
00051 }
00052 
00053 ////////////////////////////////////////////////////////////////////
00054 //     Function: HTTPEntityTag::get_string
00055 //       Access: Published
00056 //  Description: Returns the entity tag formatted for sending to an
00057 //               HTTP server (the tag is quoted, with a conditional W/
00058 //               prefix).
00059 ////////////////////////////////////////////////////////////////////
00060 string HTTPEntityTag::
00061 get_string() const {
00062   ostringstream result;
00063   if (_weak) {
00064     result << "W/";
00065   }
00066   result << '"';
00067   
00068   for (string::const_iterator ti = _tag.begin(); ti != _tag.end(); ++ti) {
00069     switch (*ti) {
00070     case '"':
00071     case '\\':
00072       result << '\\';
00073       // fall through
00074 
00075     default:
00076       result << (*ti);
00077     }
00078   }
00079 
00080   result << '"';
00081 
00082   return result.str();
00083 }
 All Classes Functions Variables Enumerations