PageRenderTime 2ms CodeModel.GetById 60ms app.highlight 32ms RepoModel.GetById 19ms app.codeStats 0ms

/esp/test/httptest/main.cpp

http://github.com/hpcc-systems/HPCC-Platform
C++ | 303 lines | 270 code | 17 blank | 16 comment | 73 complexity | a3a00541ec7ca50753082f072380f704 MD5 | raw file
  1/*##############################################################################
  2
  3    HPCC SYSTEMS software Copyright (C) 2012 HPCC Systems®.
  4
  5    Licensed under the Apache License, Version 2.0 (the "License");
  6    you may not use this file except in compliance with the License.
  7    You may obtain a copy of the License at
  8
  9       http://www.apache.org/licenses/LICENSE-2.0
 10
 11    Unless required by applicable law or agreed to in writing, software
 12    distributed under the License is distributed on an "AS IS" BASIS,
 13    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 14    See the License for the specific language governing permissions and
 15    limitations under the License.
 16############################################################################## */
 17
 18#include "httptest.hpp"
 19
 20void usage()
 21{
 22    puts("Usage:");
 23    puts("   httptest [optins]");
 24    puts("Options:");
 25    puts("   -c:  start as a client, which is the default");
 26    puts("   -s:  start as a server");
 27    puts("   -x:  start as a proxy");
 28    puts("   -r <times> :  number of times for the client to send the request");
 29    puts("   -t <number-of-threads> :  number of concurrent threads for the client to send the request");
 30    puts("   -i <filename> : input file name the client uses as request, or the server uses as response");
 31    puts("   -o <filename> : out file name");
 32    puts("   -url <[http|https]://[user:passwd@]host:port/path>: when starting as a client, the url to request");
 33    puts("   -soap: add soap http headers to the input text, must be used together with -url");
 34    puts("   -action <soap-action> ");
 35    puts("   -h <host> : hostname/ip the client connects to");
 36    puts("   -p <port> : port the client connects to, or the server listens on");
 37    puts("   -lp <port> : for proxy mode, local port that the proxy listens on");
 38    puts("   -ssl : tells it to use https instead of http");
 39    puts("   -delay <seconds>: when used as a client, delay several seconds before sending the request");
 40    puts("   -dr/-ds/-dc <seconds>: used in server mode, delay before receive, send, and close");
 41    puts("   -sc <ssl-cofig-file> : the config file that ssl uses");
 42    puts("   -d <trace-level> : 0 no tracing, > 5 some tracing, > 10 all tracing. Default is 20");
 43    puts("");
 44    puts("sample config-file: ");
 45    puts("<EspProtocol name=\"https\" type=\"secure_http_protocol\" plugin=\"esphttp.dll\">");
 46    puts("  <certificate>certificate.cer</certificate>");
 47    puts("  <privatekey>privatekey.cer</privatekey>");
 48    puts("  <passphrase>quOmuY55ftGrdcRi2y70eQ==</passphrase>");
 49    puts("  <verify enable=\"true\" address_match=\"true\" accept_selfsigned=\"true\">");
 50    puts("      <ca_certificates path=\"ca\"/>");
 51    puts("      <revoked_certificates path=\"revoked.pem\"/>");
 52    puts("      <trusted_peers>");
 53    puts("          <peer cn=\"texas.distrix.com\"/>");
 54    puts("          <peer cn=\"california.distrix.com\"/>");
 55    puts("          <peer cn=\"ymaxp\"/>");
 56    puts("      </trusted_peers>");
 57    puts("  </verify>");
 58    puts("</EspProtocol>");
 59    puts("");
 60    puts("Common use cases:");
 61    puts("-Http client to get a certain url:");
 62    puts("  httptest -c -url http://images.google.com/imghp?hl=en&tab=gi&q=");
 63    puts("-Read in the request from a file which contains the http headers and body:");
 64    puts("  httptest -c -h 10.150.51.27 -p 8010 -i spray.txt");
 65    puts("-Read in request body from a file, add soap headers, then send it:");
 66    puts("  httptest -c -url http://username:password@10.150.51.27:8010/FilSpray -action http://10.150.51.27:8010/FileSpray/Spray -i sprayxml.txt");
 67    puts("-Httpserver to display requests:");
 68    puts("  httptest -s -p 8080");
 69    puts("-Proxy to forward all incoming requests to a certain ip/port");
 70    puts("  httptest -x -lp 8010 -h 10.150.51.27 -p 8010");
 71    puts("-Fully functional proxy:");
 72    puts("  httptest -x -lp 8080");
 73
 74    exit(-1);
 75}
 76
 77enum InstanceType
 78{
 79    IT_UNKNOWN = 0,
 80    HTTPSERVER = 1,
 81    HTTPCLIENT = 2,
 82    HTTPPROXY = 3
 83};
 84
 85int main(int argc, char* argv[])
 86{
 87    InitModuleObjects();
 88
 89    InstanceType itype = IT_UNKNOWN;
 90
 91    int times = 1;
 92    int threads = 1;
 93    StringBuffer in_fname;
 94    StringBuffer out_fname;
 95    StringBuffer host;
 96    int port = 80;
 97    int localport = 80;
 98    bool use_ssl = false;
 99    bool add_soap_headers = false;
100    StringBuffer scfname;
101    StringBuffer url;
102
103    const char* soapaction = NULL;
104
105    int delay = 0;
106    int recvDelay = 0, sendDelay = 0, blockDelay = 0;
107    
108    int i = 1;
109    while(i<argc)
110    {
111        if (stricmp(argv[i], "-s")==0)
112        {
113            itype = HTTPSERVER;
114            i++;
115        }
116        else if (stricmp(argv[i], "-c")==0)
117        {
118            itype = HTTPCLIENT;
119            i++;
120        }
121        else if (stricmp(argv[i], "-x")==0)
122        {
123            itype = HTTPPROXY;
124            i++;
125        }
126        else if (stricmp(argv[i],"-r")==0)
127        {
128            i++;
129            times = atoi(argv[i++]);
130        }
131        else if (stricmp(argv[i],"-t")==0)
132        {
133            i++;
134            threads = atoi(argv[i++]);
135        }
136        else if (stricmp(argv[i], "-h")==0)
137        {
138            i++;
139            host.clear().append(argv[i++]);
140        }
141        else if (stricmp(argv[i], "-p")==0)
142        {
143            i++;
144            port = atoi(argv[i++]);
145        }
146        else if (stricmp(argv[i], "-i") == 0)
147        {
148            i++;
149            in_fname.clear().append(argv[i++]);
150        }
151        else if (stricmp(argv[i], "-o") == 0)
152        {
153            i++;
154            out_fname.clear().append(argv[i++]);
155        }
156        else if (stricmp(argv[i], "-ssl") == 0)
157        {
158            use_ssl = true;
159            i++;
160        }
161        else if (stricmp(argv[i], "-sc") == 0)
162        {
163            i++;
164            scfname.clear().append(argv[i++]);
165        }
166        else if (stricmp(argv[i], "-lp") == 0)
167        {
168            i++;
169            localport = atoi(argv[i++]);
170        }
171        else if (stricmp(argv[i], "-delay") == 0)
172        {
173            i++;
174            delay = atoi(argv[i++]);
175        }
176        else if (stricmp(argv[i], "-dr") == 0)
177        {
178            i++;
179            recvDelay = atoi(argv[i++]);
180        }
181        else if (stricmp(argv[i], "-ds") == 0)
182        {
183            i++;
184            sendDelay = atoi(argv[i++]);
185        }
186        else if (stricmp(argv[i], "-dc") == 0)
187        {
188            i++;
189            blockDelay = atoi(argv[i++]);
190        }
191        else if(stricmp(argv[i], "-url") == 0)
192        {
193            i++;
194            url.append(argv[i++]);
195        }
196        else if (stricmp(argv[i], "-soap")==0)
197        {
198            add_soap_headers = true;
199            i++;
200        }
201        else if (stricmp(argv[i], "-action")==0)
202        {
203            i++;
204            soapaction = argv[i++];
205        }
206        else if (stricmp(argv[i], "-d")==0)
207        {
208            i++;
209            httptest_tracelevel = atoi(argv[i++]);;
210        }
211        else
212        {
213            printf("Error: command format error\n");
214            usage();
215        }
216    }
217
218    try
219    {
220        Owned<IPropertyTree> sslconfig;
221        if(scfname.length() > 0)
222            sslconfig.setown(createPTreeFromXMLFile(scfname.str(), ipt_caseInsensitive));
223        FILE* ofile = NULL;
224        if(out_fname.length() != 0)
225        {
226            ofile = fopen(out_fname.str(), "a+");
227            if(ofile == NULL)
228            {
229                printf("can't open file %s\n", out_fname.str());
230                exit(-1);
231            }
232        }
233        else
234        {
235            ofile = stdout;
236        }
237
238        if(itype == HTTPSERVER)
239        {
240            HttpServer server(port, in_fname.str(), ofile, use_ssl, sslconfig.get());
241            server.setDelays(recvDelay, sendDelay, blockDelay);
242            server.start();
243        }
244        else if(itype == HTTPPROXY)
245        {
246            HttpProxy proxy(localport, host.str(), port, ofile, use_ssl, sslconfig.get());
247            proxy.start();
248        }
249        else
250        {
251            if(add_soap_headers && url.length() == 0)
252            {
253                printf("Error: when you use -soap option, you must provide the full url\ntype in \"%s -h\" for usage", argv[0]);
254                return 0;
255            }
256
257            if(host.length() == 0 && url.length() == 0)
258            {
259                printf("Error: destination host or url required\n");
260                usage();
261            }
262
263
264            if(add_soap_headers)
265            {
266                HttpClient client(threads, times, ofile);
267                if(delay > 0)
268                    client.setDelay(delay);
269                client.sendSoapRequest(url.str(), soapaction, in_fname.str());
270            }
271            else if(url.length() == 0)
272            {
273                HttpClient client(threads, times, host.str(), port, ofile, use_ssl, sslconfig.get());
274                if(delay > 0)
275                    client.setDelay(delay);
276                client.sendRequest(in_fname.str());         
277            }
278            else
279            {
280                HttpClient client(threads, times, ofile);
281                if(delay > 0)
282                    client.setDelay(delay);
283                client.getUrl(url.str());
284            }
285        }
286        fclose(ofile);
287    }
288    catch(IException *excpt)
289    {
290        StringBuffer errMsg;
291        IERRLOG("Error - %d:%s", excpt->errorCode(), excpt->errorMessage(errMsg).str());
292        return -1;
293    }
294    catch(...)
295    {
296        IERRLOG("Unknown exception");
297        return -1;
298    }
299
300    releaseAtoms();
301    return 0;
302}
303