📄 cgitest.cpp
字号:
PrintIndexes(indexes); } CNcbiIstream* is = CCR.GetInputStream(); if ( is ) { NcbiCout << "\nUn-parsed content body:\n"; NcbiCout << is->rdbuf() << NcbiEndl << NcbiEndl; }}static void TestCgiMisc(void){ const string str("_ _%_;_\n_:_'_*_\\_\"_"); {{ string url = "qwerty"; url = URL_EncodeString(str); NcbiCout << str << NcbiEndl << url << NcbiEndl; assert( url.compare("_+_%25_%3B_%0A_%3A_'_*_%5C_%22_") == 0 ); string str1 = URL_DecodeString(url); assert( str1 == str ); string url1 = URL_EncodeString(str1); assert( url1 == url ); }} {{ string url = "qwerty"; url = URL_EncodeString(str, eUrlEncode_ProcessMarkChars); NcbiCout << str << NcbiEndl << url << NcbiEndl; assert( url.compare("%5F+%5F%25%5F%3B%5F%0A%5F%3A%5F%27%5F%2A%5F%5C%5F%22%5F") == 0 ); string str1 = URL_DecodeString(url); assert( str1 == str ); string url1 = URL_EncodeString(str1, eUrlEncode_ProcessMarkChars); assert( url1 == url ); }} const string bad_url("%ax"); try { URL_DecodeString(bad_url); } STD_CATCH("%ax");}static void TestCgi(const CNcbiArguments& args){ // this is to get rid of warnings on some strict compilers (like SUN Forte)#define X_PUTENV(s) ::putenv((char*) s) TestCgi_Cookies(); TestCgi_Request_Static(); assert( !X_PUTENV("CONTENT_TYPE=application/x-www-form-urlencoded") ); try { // POST only char inp_str[] = "post11=val11&post12void=&post13=val13"; CNcbiIstrstream istr(inp_str); char len[32]; assert(::sprintf(len, "CONTENT_LENGTH=%ld", (long) ::strlen(inp_str))); assert( !::putenv(len) ); assert( !X_PUTENV("SERVER_PORT=") ); assert( !X_PUTENV("REMOTE_ADDRESS=") ); assert( !X_PUTENV("REQUEST_METHOD=POST") ); assert( !X_PUTENV("QUERY_STRING=") ); assert( !X_PUTENV("HTTP_COOKIE=") ); TestCgi_Request_Full(&istr); } STD_CATCH("TestCgi(POST only)"); try { // POST, fDoNotParseContent char inp_str[] = "post11=val11&post12void=&post13=val13"; CNcbiIstrstream istr(inp_str); char len[32]; assert(::sprintf(len, "CONTENT_LENGTH=%ld", (long) ::strlen(inp_str))); assert( !::putenv(len) ); assert( !X_PUTENV("SERVER_PORT=") ); assert( !X_PUTENV("REMOTE_ADDRESS=") ); assert( !X_PUTENV("REQUEST_METHOD=POST") ); assert( !X_PUTENV("QUERY_STRING=") ); assert( !X_PUTENV("HTTP_COOKIE=") ); TestCgi_Request_Full(&istr, 0, CCgiRequest::fDoNotParseContent); } STD_CATCH("TestCgi(POST only)"); try { // POST + aux. functions char inp_str[] = "post22void=&post23void="; CNcbiIstrstream istr(inp_str); char len[32]; assert(::sprintf(len, "CONTENT_LENGTH=%ld", (long) ::strlen(inp_str))); assert( !::putenv(len) ); assert( !X_PUTENV("SERVER_PORT=9999") ); assert( !X_PUTENV("HTTP_USER_AGENT=MyUserAgent") ); assert( !X_PUTENV("HTTP_MY_RANDOM_PROP=MyRandomPropValue") ); assert( !X_PUTENV("REMOTE_ADDRESS=130.14.25.129") ); TestCgi_Request_Full(&istr); } STD_CATCH("TestCgi(POST + aux. functions)"); // this is for all following tests... char inp_str[] = "postXXX=valXXX"; char len[32]; assert( ::sprintf(len, "CONTENT_LENGTH=%ld", (long) ::strlen(inp_str)) ); assert( !::putenv(len) ); try { // POST + ISINDEX(action) CNcbiIstrstream istr(inp_str); assert( !X_PUTENV("QUERY_STRING=isidx1+isidx2+isidx3") ); TestCgi_Request_Full(&istr); } STD_CATCH("TestCgi(POST + ISINDEX(action))"); try { // POST + QUERY(action) CNcbiIstrstream istr(inp_str); assert( !X_PUTENV("QUERY_STRING=query1=vv1&query2=") ); TestCgi_Request_Full(&istr); } STD_CATCH("TestCgi(POST + QUERY(action))"); try { // GET ISINDEX + COOKIES CNcbiIstrstream istr(inp_str); assert( !X_PUTENV("QUERY_STRING=get_isidx1+get_isidx2+get_isidx3") ); assert( !X_PUTENV("HTTP_COOKIE=cook1=val1; cook2=val2;") ); TestCgi_Request_Full(&istr, 0, CCgiRequest::fIndexesNotEntries); } STD_CATCH("TestCgi(GET ISINDEX + COOKIES)"); try { // GET REGULAR, NO '=' CNcbiIstrstream istr(inp_str); assert( !X_PUTENV("QUERY_STRING=get_query1_empty&get_query2_empty") ); TestCgi_Request_Full(&istr); } STD_CATCH("TestCgi(GET REGULAR, NO '=' )"); try { // GET REGULAR + COOKIES CNcbiIstrstream istr(inp_str); assert( !X_PUTENV("QUERY_STRING=get_query1=gq1&get_query2=") ); assert( !X_PUTENV("HTTP_COOKIE=_cook1=_val1;_cook2=_val2") ); TestCgi_Request_Full(&istr); } STD_CATCH("TestCgi(GET REGULAR + COOKIES)"); try { // ERRONEOUS STDIN CNcbiIstrstream istr("123"); assert( !X_PUTENV("QUERY_STRING=get_query1=gq1&get_query2=") ); assert( !X_PUTENV("HTTP_COOKIE=_cook1=_val1;_cook2=_val2") ); TestCgi_Request_Full(&istr); } STD_CATCH("TestCgi(ERRONEOUS STDIN)"); try { // USER INPUT(real STDIN) assert( !X_PUTENV("QUERY_STRING=u_query1=uq1") ); assert( !X_PUTENV("HTTP_COOKIE=u_cook1=u_val1; u_cook2=u_val2") ); assert( !X_PUTENV("REQUEST_METHOD=POST") ); NcbiCout << "Enter the length of CGI posted data now: " << NcbiFlush; long l = 0; if (!(NcbiCin >> l) || len < 0) { NcbiCin.clear(); runtime_error("Invalid length of CGI posted data"); } char cs[32]; assert( ::sprintf(cs, "CONTENT_LENGTH=%ld", (long) l) ); assert( !X_PUTENV(cs) ); NcbiCout << "Enter the CGI posted data now(no spaces): " << NcbiFlush; NcbiCin >> NcbiWs; TestCgi_Request_Full(0); NcbiCin.clear(); } STD_CATCH("TestCgi(USER STDIN)"); try { // CMD.-LINE ARGS assert( !X_PUTENV("REQUEST_METHOD=") ); assert( !X_PUTENV("QUERY_STRING=MUST NOT BE USED HERE!!!") ); TestCgi_Request_Full(&NcbiCin/* dummy */, &args); } STD_CATCH("TestCgi(CMD.-LINE ARGS)"); TestCgiMisc();#undef X_PUTENV}static void TestCgiResponse(const CNcbiArguments& args){ NcbiCout << "Starting CCgiResponse test" << NcbiEndl; CCgiResponse response; response.SetOutput(&NcbiCout); if (args.Size() > 2) { CCgiCookies cookies(args[2]); response.Cookies().Add(cookies); } response.Cookies().Remove(response.Cookies().Find("to-Remove")); NcbiCout << "Cookies: " << response.Cookies() << NcbiEndl; NcbiCout << "Generated simple HTTP response:" << NcbiEndl; response.WriteHeader() << "Data1" << NcbiEndl << NcbiFlush; response.out() << "Data2" << NcbiEndl << NcbiFlush; NcbiCout << "End of simple HTTP response" << NcbiEndl << NcbiEndl; response.SetHeaderValue("Some-Header-Name", "Some Header Value"); response.SetHeaderValue("status", "399 Something is BAAAAD!!!!!"); response.SetStatus(301, "Moved"); NcbiCout << "Generated HTTP response:" << NcbiEndl; response.WriteHeader() << "Data1" << NcbiEndl << NcbiFlush; response.out() << "Data2" << NcbiEndl << NcbiFlush; NcbiCout << "End of HTTP response" << NcbiEndl << NcbiEndl; response.SetRawCgi(true); NcbiCout << "Generated HTTP \"raw CGI\" response:" << NcbiEndl; response.WriteHeader() << "Data1" << NcbiEndl << NcbiFlush; response.out() << "Data2" << NcbiEndl << NcbiFlush; NcbiCout << "End of HTTP \"raw CGI\" response" << NcbiEndl << NcbiEndl;}/////////////////////////////////// Test CGI application//class CTestApplication : public CNcbiApplication{public: virtual ~CTestApplication(void); virtual int Run(void);};int CTestApplication::Run(void){ TestCgi( GetArguments() ); TestCgiResponse( GetArguments() ); return 0;}CTestApplication::~CTestApplication(){ SetDiagStream(0);}/////////////////////////////////// APPLICATION OBJECT// and// MAIN//// Note that if the application's object ("theTestApplication") was defined// inside the scope of function "main()", then its destructor could be// called *before* destructors of other statically allocated objects// defined in other modules.// It would cause a premature closure of diag. stream, and disallow the// destructors of other projects to refer to this application object:// - the singleton method CNcbiApplication::Instance() would return NULL, and// - if there is a "raw"(direct) pointer to "theTestApplication" then it// might cause a real trouble.static CTestApplication theTestApplication;int main(int argc, const char* argv[]){ // Execute main application function return theTestApplication.AppMain(argc, argv);}/* * ========================================================================== * $Log: cgitest.cpp,v $ * Revision 1000.2 2004/06/01 18:39:31 gouriano * PRODUCTION: UPGRADED [GCC34_MSVC7] Dev-tree R1.20 * * Revision 1.20 2004/05/17 20:57:14 gorelenk * Added include of PCH ncbi_pch.hpp * * Revision 1.19 2004/04/26 16:39:34 ucko * Don't try to pass temporary CCgiCookies objects, even by const * reference, as CCgiCookies has no public copy constructor. * * Revision 1.18 2003/11/24 18:15:21 ucko * Verify & -> & conversion. * * Revision 1.17 2003/08/20 22:48:38 ucko * Allow equal signs in values. * * Revision 1.16 2003/07/08 19:07:28 ivanov * Added into TestCgi() test for URL_Encode() mark characters encoding * * Revision 1.15 2002/12/30 21:13:29 vakatov * Cosmetics and compiler warning elimination * * Revision 1.14 2002/07/10 18:41:53 ucko * Drop unnecessary uses of c_str(). * Adapt slightly for CCgiEntry. * * Revision 1.13 2002/04/16 18:47:10 ivanov * Centralize threatment of assert() in tests. * Added #include <test/test_assert.h>. CVS log moved to end of file. * * Revision 1.12 2002/03 /19 00:36:20 vakatov * TestCgiResponse() -- three tests (and more extended ones) in the place * of one * * Revision 1.11 2001/12/06 00:31:51 vakatov * CCgiRequest::ParseEntries() -- allow leading '&' in the query string (temp.) * * Revision 1.10 2001/06/19 20:08:31 vakatov * CCgiRequest::{Set,Get}InputStream() -- to provide safe access to the * requests' content body * * Revision 1.9 2001/01/30 23:17:33 vakatov * + CCgiRequest::GetEntry() * * Revision 1.8 2000/06/26 16:35:25 vakatov * Test for CCgiCookies::Add(const string&), which is now maimed to * workaround MS IE bug * (it sent empty cookies w/o "=" in versions prior to 5.5) * * Revision 1.7 2000/02/01 22:19:58 vakatov * CCgiRequest::GetRandomProperty() -- allow to retrieve value of * properties whose names are not prefixed by "HTTP_" (optional). * Get rid of the aux.methods GetServerPort() and GetRemoteAddr() which * are obviously not widely used (but add to the volume of API). * * Revision 1.6 2000/01/20 17:55:46 vakatov * Fixes to follow the "CNcbiApplication" change. * * Revision 1.5 1999/12/30 22:06:14 vakatov * TestCgi() -- use $CONTENT_TYPE * * Revision 1.4 1999/11/22 19:57:09 vakatov * TestCgiResponse() -- made "static" * * Revision 1.3 1999/11/12 17:50:05 vakatov * To be more careful with _DEBUG to suit some ugly MSVC++ features * * Revision 1.2 1999/11/02 20:35:44 vakatov * Redesigned of CCgiCookie and CCgiCookies to make them closer to the * cookie standard, smarter, and easier in use * * Revision 1.1 1999/05/11 03:11:57 vakatov * Moved the CGI API(along with the relevant tests) from "corelib/" to "cgi/" * * ========================================================================== */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -