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

Go to the SVN repository for this file.

1 #include <ncbi_pch.hpp>
2 
3 #include <cgi/cgiapp.hpp>
4 #include <cgi/cgictx.hpp>
5 #include <html/html.hpp>
6 #include <html/page.hpp>
7 
8 #include "deployable_cgi.hpp"
9 
11 
12 
13 #define NEED_SET_DEPLOYMENT_UID
14 
15 static const char* CD_VERSION_ENV = "APPLICATION_VERSION";
16 static const char* CD_VERSION_UNKNOWN = "unknown";
17 
18 
20 {
21  bool found = false;
22  const string version = GetEnvironment().Get(CD_VERSION_ENV, &found);
23  if (!found) {
24  return CD_VERSION_UNKNOWN;
25  }
26  return version;
27 }
28 
29 
31 {
32  const CCgiRequest& request = ctx.GetRequest();
33  CCgiResponse& response = ctx.GetResponse();
34 
35 #ifdef NEED_SET_DEPLOYMENT_UID
36  const auto deployment_uid = GetEnvironment().Get("DEPLOYMENT_UID");
37  CDiagContext &diag_ctx(GetDiagContext());
38  CDiagContext_Extra extra(diag_ctx.Extra());
39  extra.Print("deployment_uid", deployment_uid);
40 #endif /* NEED_SET_DEPLOYMENT_UID */
41 
42  response.SetStatus(200);
43  response.WriteHeader();
44 
45  unique_ptr<CHTMLPage> page;
46  try {
47  page.reset(new CHTMLPage("Sample CGI", "./share/deployable_cgi_env.html"));
48  } catch (const exception& e) {
49  ERR_POST("Failed to create Sample CGI HTML page: " << e.what());
50  return 2;
51  }
52 
53  try {
54  list<string> names;
56  env->HeaderCell(0,0)->AppendPlainText("Name");
57  env->HeaderCell(0,1)->AppendPlainText("Value");
58 
60 
61  int row=1;
62  for(auto name : names) {
63  env->Cell(row,0)->AppendPlainText(name);
64  env->Cell(row,1)->AppendPlainText(GetEnvironment().Get(name));
65  ++row;
66  }
67  page->AddTagMap("ENVIRONMENT", env);
68  }
69  catch (const exception& e) {
70  ERR_POST("Failed to populate Sample CGI HTML page: " << e.what());
71  return 3;
72  }
73 
74  try {
76  page->Print(response.out(), CNCBINode::eHTML);
77  }
78  } catch (const CCgiHeadException& ex) {
79  ERR_POST("CCgiHeadException" << ex.what());
80  throw;
81  } catch (const exception& e) {
82  ERR_POST("exception: " << e.what());
83  ERR_POST("Failed to compose/send Sample CGI HTML page: " << e.what());
84  return 4;
85  }
86  return 0;
87 }
88 
89 
91 {
92  // Parse, verify, and look at cmd-line and CGI parameters via "CArgs"
93  // (optional)
94  x_LookAtArgs();
95 
96  // Given "CGI context", get access to its "HTTP request" and
97  // "HTTP response" sub-objects
98  const CCgiRequest& request = ctx.GetRequest();
99  CCgiResponse& response = ctx.GetResponse();
100 
101  string path_info = request.GetProperty(eCgi_PathInfo);
102  NStr::TrimSuffixInPlace(path_info, "/");
103  NStr::TrimPrefixInPlace(path_info, "/");
104  if ( path_info == "environment") {
106  } else if(path_info=="crash") {
107  int *p = (int*)0;
108  return *p;
109  }
110 
111 #ifdef NEED_SET_DEPLOYMENT_UID
112  const auto deployment_uid = GetEnvironment().Get("DEPLOYMENT_UID");
113  CDiagContext &diag_ctx(GetDiagContext());
114  CDiagContext_Extra extra(diag_ctx.Extra());
115  extra.Print("deployment_uid", deployment_uid);
116 #endif /* NEED_SET_DEPLOYMENT_UID */
117  response.SetStatus(200);
118  response.WriteHeader();
119 
120  // Try to retrieve the message ('message=...') from the HTTP request.
121  // NOTE: the case sensitivity was turned off in Init().
122  bool is_message = false;
123  string message = request.GetEntry("Message", &is_message);
124  if ( is_message ) {
125  message = "'" + message + "'";
126  } else {
127  message = "";
128  }
129 
130  // NOTE: While this sample uses the CHTML* classes for generating HTML,
131  // you are encouraged to use XML/XSLT and the NCBI port of XmlWrapp.
132  // For more info:
133  // https://ncbi.github.io/cxx-toolkit/pages/ch_xmlwrapp
134  // https://www.ncbi.nlm.nih.gov/IEB/ToolBox/CPP_DOC/doxyhtml/namespacexml.html
135 
136  // Create a HTML page (using template HTML file "deployable_cgi.html")
137  unique_ptr<CHTMLPage> page;
138  try {
139  page.reset(new CHTMLPage("Sample CGI", "./share/deployable_cgi.html"));
140  } catch (const exception& e) {
141  ERR_POST("Failed to create Sample CGI HTML page: " << e.what());
142  return 2;
143  }
144 
145 #ifdef NEED_SET_DEPLOYMENT_UID
146  CHTMLPlainText *depuid_text = new CHTMLPlainText(deployment_uid);
147  page->AddTagMap("DEPUID", depuid_text);
148 #endif /* NEED_SET_DEPLOYMENT_UID */
149  // Register substitution for the template parameters <@MESSAGE@> and
150  // <@SELF_URL@>
151  try {
152  _TRACE("Substituting templates");
153  page->AddTagMap("MESSAGE", new CHTMLPlainText(message));
154  page->AddTagMap("SELF_URL", new CHTMLPlainText(ctx.GetSelfURL()));
155  page->AddTagMap("TITLE", new CHTMLPlainText("C++ SVN CGI Sample"));
156  page->AddTagMap("VERSION", new CHTMLPlainText(GetCdVersion()));
157  }
158  catch (const exception& e) {
159  ERR_POST("Failed to populate Sample CGI HTML page: " << e.what());
160  return 3;
161  }
162 
163  // Compose and flush the resultant HTML page
164  try {
165  _TRACE("stream status: " << ctx.GetStreamStatus());
166  if (request.GetRequestMethod() != CCgiRequest::eMethod_HEAD) {
167  _TRACE("printing response");
168  page->Print(response.out(), CNCBINode::eHTML);
169  _TRACE("done printing response");
170  }
171  } catch (const CCgiHeadException& ex) {
172  ERR_POST("CCgiHeadException" << ex.what());
173  throw;
174  } catch (const exception& e) {
175  ERR_POST("exception: " << e.what());
176  ERR_POST("Failed to compose/send Sample CGI HTML page: " << e.what());
177  return 4;
178  }
179 
180  _TRACE("End of execution");
181  return 0;
182 }
183 
CCgiHeadException –.
CCgiRequest::
Definition: ncbicgi.hpp:685
int ProcessPrintEnvironment(ncbi::CCgiContext &ctx)
Definition: process.cpp:30
const std::string GetCdVersion() const
Definition: process.cpp:19
virtual int ProcessRequest(CCgiContext &ctx)
This is the method you should override.
Definition: cgi_sample.cpp:83
Temporary object for holding extra message arguments.
Definition: ncbidiag.hpp:1828
CHTMLPage –.
Definition: page.hpp:161
CRef –.
Definition: ncbiobj.hpp:618
CS_CONTEXT * ctx
Definition: t0006.c:12
static const struct name_t names[]
const CNcbiEnvironment & GetEnvironment(void) const
Get the application's cached environment.
CNcbiOstream & out(void) const
Get output stream. Throw exception if GetOutput() is NULL.
Definition: ncbicgir.cpp:257
void SetStatus(unsigned int code, const string &reason=kEmptyStr)
Definition: ncbicgir.cpp:197
const string & GetProperty(ECgiProp prop) const
Get value of a "standard" property (return empty string if not defined)
Definition: ncbicgi.cpp:1432
CNcbiOstream & WriteHeader(void) const
Write HTTP response header to the output stream.
Definition: ncbicgir.hpp:396
const CCgiEntry & GetEntry(const string &name, bool *is_found=0) const
Get entry value by name.
Definition: ncbicgi.cpp:1449
ERequestMethod GetRequestMethod(void) const
Get request method.
Definition: ncbicgi.cpp:1810
@ eCgi_PathInfo
Definition: ncbicgi.hpp:396
#define _TRACE(message)
Definition: ncbidbg.hpp:122
CDiagContext_Extra & Print(const string &name, const string &value)
The method does not print the argument, but adds it to the string.
Definition: ncbidiag.cpp:2622
CDiagContext & GetDiagContext(void)
Get diag context instance.
Definition: logging.cpp:818
CDiagContext_Extra Extra(void) const
Create a temporary CDiagContext_Extra object.
Definition: ncbidiag.hpp:2095
#define ERR_POST(message)
Error posting with file, line number information but without error codes.
Definition: ncbidiag.hpp:186
const string & Get(const string &name, bool *found=NULL) const
Get environment value by name.
Definition: ncbienv.cpp:109
void Enumerate(list< string > &names, const string &prefix=kEmptyStr) const
Find all variable names starting with an optional prefix.
Definition: ncbienv.cpp:133
@ eHTML
Definition: node.hpp:109
static void TrimSuffixInPlace(string &str, const CTempString suffix, ECase use_case=eCase)
Trim suffix from a string (in-place)
Definition: ncbistr.cpp:3274
static void TrimPrefixInPlace(string &str, const CTempString prefix, ECase use_case=eCase)
Trim prefix from a string (in-place)
Definition: ncbistr.cpp:3238
HTML classes.
static int version
Definition: mdb_load.c:29
const TYPE & Get(const CNamedParameterList *param)
The HTML page.
static const char * CD_VERSION_ENV
Definition: process.cpp:15
USING_NCBI_SCOPE
Definition: process.cpp:10
static const char * CD_VERSION_UNKNOWN
Definition: process.cpp:16
static HENV env
Definition: transaction2.c:38
Modified on Fri Mar 01 10:05:10 2024 by modify_doxy.py rev. 669887