NCBI C++ ToolKit
cgi_serial.cpp
Go to the documentation of this file.

Go to the SVN repository for this file.

1 /* $Id: cgi_serial.cpp 45745 2010-05-12 18:16:26Z ivanov $
2  * ===========================================================================
3  *
4  * PUBLIC DOMAIN NOTICE
5  * National Center for Biotechnology Information
6  *
7  * This software/database is a "United States Government Work" under the
8  * terms of the United States Copyright Act. It was written as part of
9  * the author's official duties as a United States Government employee and
10  * thus cannot be copyrighted. This software/database is freely available
11  * to the public for use. The National Library of Medicine and the U.S.
12  * Government have not placed any restriction on its use or reproduction.
13  *
14  * Although all reasonable efforts have been taken to ensure the accuracy
15  * and reliability of the software and data, the NLM and the U.S.
16  * Government do not and cannot warrant the performance or results that
17  * may be obtained by using this software or data. The NLM and the U.S.
18  * Government disclaim all warranties, express or implied, including
19  * warranties of performance, merchantability or fitness for any particular
20  * purpose.
21  *
22  * Please cite the author in any work or product based on this material.
23  *
24  * ===========================================================================
25  *
26  * Author: Eugene Vasilchenko, Denis Vakatov
27  *
28  * File Description:
29  *
30  * ===========================================================================
31  */
32 
33 #include <ncbi_pch.hpp>
34 #include <corelib/ncbienv.hpp>
35 #include <corelib/ncbimisc.hpp>
36 #include <cgi/cgi_serial.hpp>
37 #include <cgi/ncbicgi.hpp>
38 
39 
41 
43 {
44  size_t pos = str.find('|');
45  string ssize = str.substr(0,pos);
46  size_t size = NStr::StringToUInt(ssize);
47  string value = str.substr(pos+1,size);
48  pos = pos + 1 + size;
49  size_t pos1 = str.find('|', pos);
50  ssize = str.substr(pos, pos1-pos);
51  size = NStr::StringToUInt(ssize);
52  pos1 = pos1+1+size;
53  string fname = str.substr(pos1,size);
54  pos = str.find('|', pos1);
55  ssize = str.substr(pos1, pos-pos1);
56  size = NStr::StringToUInt(ssize);
57  string type = str.substr(pos+1,size);
58  ssize = str.substr(pos+1+size);
59  unsigned int position = NStr::StringToUInt(ssize);
60  return CCgiEntry(value,fname,position,type);
61 }
62 
64 {
65  string ret = NStr::UIntToString((unsigned int)elem.GetValue().length()) + '|';
66  ret += elem.GetValue();
67  ret += NStr::UIntToString((unsigned int)elem.GetFilename().length()) + '|';
68  ret += elem.GetFilename();
69  ret += NStr::UIntToString((unsigned int)elem.GetContentType().length()) + '|';
70  ret += elem.GetContentType();
71  ret += NStr::UIntToString(elem.GetPosition());
72  return ret;
73 }
74 
75 
76 //////////////////////////////////////////////////////////////////////////////
77 ///
78 
80 {
81  COStreamHelper ostr(os);
82  cont.Write(ostr, CCgiCookie::eHTTPRequest);
83  ostr.flush(true);
84  return os;
85 }
86 
88 {
89  string str = ReadStringFromStream(is);
90  cont.Clear();
91  cont.Add(str);
92  return is;
93 }
94 
95 //////////////////////////////////////////////////////////////////////////////
96 ///
99 {
100  list<string> names;
101  cont.Enumerate(names);
102  TVars vars;
103  ITERATE(list<string>, it, names) {
104  string var = cont.Get(*it);
105  if (!var.empty())
106  vars[*it] = var;
107  }
108  WriteMap(os, vars);
109  return os;
110 }
112 {
113  TVars vars;
114  ReadMap(is, vars);
115  if (vars.empty()) {
116  cont.Reset();
117  return is;
118  }
119  AutoPtr<const char*, ArrayDeleter<const char*> > env( new const char*[vars.size()+1]);
120  vector<string> strings;
121  strings.reserve(vars.size());
122  size_t index = 0;
123  ITERATE(TVars, it, vars) {
124  strings.push_back( it->first + '=' + it->second);
125  (env.get()[index]) = strings[index].c_str();
126  ++index;
127  }
128  (env.get()[index]) = NULL;
129  cont.Reset(env.get());
130  return is;
131 }
132 
133 
map< string, string > TVars
Definition: cgi_serial.cpp:97
CNcbiIstream & ReadEnvironment(CNcbiIstream &is, CNcbiEnvironment &cont)
Write an environment from a stream.
Definition: cgi_serial.cpp:111
CNcbiIstream & ReadCgiCookies(CNcbiIstream &is, CCgiCookies &cont)
Read cgi cookeis from a stream.
Definition: cgi_serial.cpp:87
CNcbiOstream & WriteEnvironment(CNcbiOstream &os, const CNcbiEnvironment &cont)
Write an environment to a stream.
Definition: cgi_serial.cpp:98
CNcbiOstream & WriteCgiCookies(CNcbiOstream &os, const CCgiCookies &cont)
Write cgi cookeis to a stream.
Definition: cgi_serial.cpp:79
CNcbiIstream & ReadMap(CNcbiIstream &is, TMap &cont)
Read a map from a stream.
Definition: cgi_serial.hpp:186
CNcbiOstream & WriteMap(CNcbiOstream &os, const TMap &cont)
Write a map to a stream.
Definition: cgi_serial.hpp:164
string ReadStringFromStream(CNcbiIstream &is)
Read a string from a stream.
Definition: cgi_serial.hpp:139
AutoPtr –.
Definition: ncbimisc.hpp:401
CCgiCookies::
Definition: ncbicgi.hpp:219
static TElem FromString(const string &str)
Definition: cgi_serial.hpp:54
static string ToString(const TElem &elem)
Definition: cgi_serial.hpp:55
CNcbiEnvironment –.
Definition: ncbienv.hpp:110
COStreamHelper.
Definition: cgi_serial.hpp:97
void flush(bool write_empty_data=false)
Definition: cgi_serial.hpp:111
size_type size() const
Definition: map.hpp:148
bool empty() const
Definition: map.hpp:149
static const struct name_t names[]
static const char * str(char *buf, int n)
Definition: stats.c:84
static HENV env
Definition: transaction2.c:38
static const char *const strings[]
Definition: utf8.c:21
#define ITERATE(Type, Var, Cont)
ITERATE macro to sequence through container elements.
Definition: ncbimisc.hpp:815
const string & GetValue() const
Get the value as a string, (necessarily) prefetching it all if applicable; the result remains availab...
Definition: ncbicgi.hpp:470
const string & GetContentType() const
May be available for some fields of POSTed forms.
Definition: ncbicgi.hpp:525
void Clear(void)
Remove all stored cookies.
Definition: ncbicgi.cpp:792
CNcbiOstream & Write(CNcbiOstream &os, CCgiCookie::EWriteMethod wmethod=CCgiCookie::eHTTPResponse) const
Printout all cookies into the stream "os".
Definition: ncbicgi.cpp:670
CCgiCookie * Add(const string &name, const string &value, const string &domain=kEmptyStr, const string &path=kEmptyStr, EOnBadCookie on_bad_cookie=eOnBadCookie_SkipAndError)
All Add() functions: if the added cookie has the same {name, domain, path} as an already existing one...
Definition: ncbicgi.cpp:383
const string & GetFilename() const
Only available for certain fields of POSTed forms.
Definition: ncbicgi.hpp:508
unsigned int GetPosition() const
CGI parameter number – automatic image name parameter is #0, explicit parameters start at #1.
Definition: ncbicgi.hpp:517
@ eHTTPRequest
Definition: ncbicgi.hpp:87
#define NULL
Definition: ncbistd.hpp:225
const string & Get(const string &name, bool *found=NULL) const
Get environment value by name.
Definition: ncbienv.cpp:109
void Reset(const char *const *envp=0)
Reset environment.
Definition: ncbienv.cpp:87
void Enumerate(list< string > &names, const string &prefix=kEmptyStr) const
Find all variable names starting with an optional prefix.
Definition: ncbienv.cpp:133
#define END_NCBI_SCOPE
End previously defined NCBI scope.
Definition: ncbistl.hpp:103
#define BEGIN_NCBI_SCOPE
Define ncbi namespace.
Definition: ncbistl.hpp:100
IO_PREFIX::ostream CNcbiOstream
Portable alias for ostream.
Definition: ncbistre.hpp:149
IO_PREFIX::istream CNcbiIstream
Portable alias for istream.
Definition: ncbistre.hpp:146
static string UIntToString(unsigned int value, TNumToStringFlags flags=0, int base=10)
Convert UInt to string.
Definition: ncbistr.hpp:5103
static unsigned int StringToUInt(const CTempString str, TStringToNumFlags flags=0, int base=10)
Convert string to unsigned int.
Definition: ncbistr.cpp:642
const struct ncbi::grid::netcache::search::fields::SIZE size
const GenericPointer< typename T::ValueType > T2 value
Definition: pointer.h:1227
Defines unified interface to application:
Miscellaneous common-use basic types and functionality.
Definition: type.c:6
Modified on Fri Sep 20 14:58:32 2024 by modify_doxy.py rev. 669887