Back to index

lightning-sunbird  0.9+nobinonly
TestServ.cpp
Go to the documentation of this file.
00001 /* vim:set ts=4 sw=4 et cindent: */
00002 /* ***** BEGIN LICENSE BLOCK *****
00003  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
00004  *
00005  * The contents of this file are subject to the Mozilla Public License Version
00006  * 1.1 (the "License"); you may not use this file except in compliance with
00007  * the License. You may obtain a copy of the License at
00008  * http://www.mozilla.org/MPL/
00009  *
00010  * Software distributed under the License is distributed on an "AS IS" basis,
00011  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
00012  * for the specific language governing rights and limitations under the
00013  * License.
00014  *
00015  * The Original Code is Mozilla.
00016  *
00017  * The Initial Developer of the Original Code is IBM Corporation.
00018  * Portions created by IBM Corporation are Copyright (C) 2003
00019  * IBM Corporation. All Rights Reserved.
00020  *
00021  * Contributor(s):
00022  *   Darin Fisher <darin@meer.net>
00023  *
00024  * Alternatively, the contents of this file may be used under the terms of
00025  * either the GNU General Public License Version 2 or later (the "GPL"), or
00026  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
00027  * in which case the provisions of the GPL or the LGPL are applicable instead
00028  * of those above. If you wish to allow use of your version of this file only
00029  * under the terms of either the GPL or the LGPL, and not to allow others to
00030  * use your version of this file under the terms of the MPL, indicate your
00031  * decision by deleting the provisions above and replace them with the notice
00032  * and other provisions required by the GPL or the LGPL. If you do not delete
00033  * the provisions above, a recipient may use your version of this file under
00034  * the terms of any one of the MPL, the GPL or the LGPL.
00035  *
00036  * ***** END LICENSE BLOCK ***** */
00037 
00038 #include "TestCommon.h"
00039 #include <stdlib.h>
00040 #include "nsIServiceManager.h"
00041 #include "nsIEventQueueService.h"
00042 #include "nsIEventQueue.h"
00043 #include "nsIServerSocket.h"
00044 #include "nsISocketTransport.h"
00045 #include "nsNetUtil.h"
00046 #include "nsString.h"
00047 #include "nsCOMPtr.h"
00048 #include "prlog.h"
00049 
00050 #if defined(PR_LOGGING)
00051 //
00052 // set NSPR_LOG_MODULES=Test:5
00053 //
00054 static PRLogModuleInfo *gTestLog = nsnull;
00055 #endif
00056 #define LOG(args) PR_LOG(gTestLog, PR_LOG_DEBUG, args)
00057 
00058 static PRBool gKeepRunning = PR_TRUE;
00059 static nsIEventQueue* gEventQ = nsnull;
00060 
00061 class MySocketListener : public nsIServerSocketListener
00062 {
00063 public:
00064     NS_DECL_ISUPPORTS
00065     NS_DECL_NSISERVERSOCKETLISTENER
00066 
00067     MySocketListener() {}
00068     virtual ~MySocketListener() {}
00069 };
00070 
00071 NS_IMPL_THREADSAFE_ISUPPORTS1(MySocketListener, nsIServerSocketListener)
00072 
00073 NS_IMETHODIMP
00074 MySocketListener::OnSocketAccepted(nsIServerSocket *serv,
00075                                    nsISocketTransport *trans)
00076 {
00077     LOG(("MySocketListener::OnSocketAccepted [serv=%p trans=%p]\n", serv, trans));
00078 
00079     nsCAutoString host;
00080     PRInt32 port;
00081 
00082     trans->GetHost(host);
00083     trans->GetPort(&port);
00084 
00085     LOG(("  -> %s:%d\n", host.get(), port));
00086 
00087     nsCOMPtr<nsIInputStream> input;
00088     nsCOMPtr<nsIOutputStream> output;
00089     nsresult rv;
00090 
00091     rv = trans->OpenInputStream(nsITransport::OPEN_BLOCKING, 0, 0, getter_AddRefs(input));
00092     if (NS_FAILED(rv))
00093         return rv;
00094 
00095     rv = trans->OpenOutputStream(nsITransport::OPEN_BLOCKING, 0, 0, getter_AddRefs(output));
00096     if (NS_FAILED(rv))
00097         return rv;
00098 
00099     char buf[256];
00100     PRUint32 n;
00101 
00102     rv = input->Read(buf, sizeof(buf), &n);
00103     if (NS_FAILED(rv))
00104         return rv;
00105 
00106     const char response[] = "HTTP/1.0 200 OK\r\nContent-Type: text/plain\r\n\r\nFooooopy!!\r\n";
00107     rv = output->Write(response, sizeof(response) - 1, &n);
00108     if (NS_FAILED(rv))
00109         return rv;
00110 
00111     input->Close();
00112     output->Close();
00113     return NS_OK;
00114 }
00115 
00116 NS_IMETHODIMP
00117 MySocketListener::OnStopListening(nsIServerSocket *serv, nsresult status)
00118 {
00119     LOG(("MySocketListener::OnStopListening [serv=%p status=%x]\n", serv, status));
00120     gKeepRunning = PR_FALSE;
00121     return NS_OK;
00122 }
00123 
00124 static nsresult
00125 MakeServer(PRInt32 port)
00126 {
00127     nsresult rv;
00128     nsCOMPtr<nsIServerSocket> serv = do_CreateInstance(NS_SERVERSOCKET_CONTRACTID, &rv);
00129     if (NS_FAILED(rv))
00130         return rv;
00131 
00132     rv = serv->Init(port, PR_TRUE, 5);
00133     if (NS_FAILED(rv))
00134         return rv;
00135 
00136     rv = serv->GetPort(&port);
00137     if (NS_FAILED(rv))
00138         return rv;
00139     LOG(("  listening on port %d\n", port));
00140 
00141     rv = serv->AsyncListen(new MySocketListener());
00142     return rv;
00143 }
00144 
00145 int
00146 main(int argc, char* argv[])
00147 {
00148     if (test_common_init(&argc, &argv) != 0)
00149         return -1;
00150 
00151     nsresult rv= (nsresult)-1;
00152     if (argc < 2) {
00153         printf("usage: %s <port>\n", argv[0]);
00154         return -1;
00155     }
00156 
00157 #if defined(PR_LOGGING)
00158     gTestLog = PR_NewLogModule("Test");
00159 #endif
00160 
00161     /* 
00162      * The following code only deals with XPCOM registration stuff. and setting
00163      * up the event queues. Copied from TestSocketIO.cpp
00164      */
00165 
00166     rv = NS_InitXPCOM2(nsnull, nsnull, nsnull);
00167     if (NS_FAILED(rv)) return rv;
00168 
00169     {
00170         // Create the Event Queue for this thread...
00171         nsCOMPtr<nsIEventQueueService> eqs =
00172                  do_GetService(NS_EVENTQUEUESERVICE_CONTRACTID, &rv);
00173         if (NS_FAILED(rv)) return rv;
00174 
00175         eqs->GetThreadEventQueue(NS_CURRENT_THREAD, &gEventQ);
00176 
00177         rv = MakeServer(atoi(argv[1]));
00178         if (NS_FAILED(rv)) {
00179             LOG(("MakeServer failed [rv=%x]\n", rv));
00180             return -1;
00181         }
00182 
00183         // Enter the message pump to allow the URL load to proceed.
00184         while (gKeepRunning) {
00185             PLEvent *event;
00186             gEventQ->WaitForEvent(&event);
00187             gEventQ->HandleEvent(event);
00188         }
00189     } // this scopes the nsCOMPtrs
00190     // no nsCOMPtrs are allowed to be alive when you call NS_ShutdownXPCOM
00191     NS_ShutdownXPCOM(nsnull);
00192     return rv;
00193 }