You can subscribe to this list here.
2003 |
Jan
|
Feb
|
Mar
|
Apr
|
May
|
Jun
|
Jul
|
Aug
|
Sep
|
Oct
|
Nov
(102) |
Dec
(255) |
---|---|---|---|---|---|---|---|---|---|---|---|---|
2004 |
Jan
(38) |
Feb
(16) |
Mar
(4) |
Apr
|
May
|
Jun
|
Jul
|
Aug
|
Sep
|
Oct
|
Nov
|
Dec
|
From: <mik...@us...> - 2003-12-27 11:56:09
|
Update of /cvsroot/sharedaemon/sharedaemon-ui-web/src/server/http In directory sc8-pr-cvs1:/tmp/cvs-serv3326/src/server/http Modified Files: HttpServerSocket.cpp Log Message: 27/12/2003 Mikael Barbeaux * Fixed a Win32 compilation bug, should work perfectly now. Index: HttpServerSocket.cpp =================================================================== RCS file: /cvsroot/sharedaemon/sharedaemon-ui-web/src/server/http/HttpServerSocket.cpp,v retrieving revision 1.5 retrieving revision 1.6 diff -u -d -r1.5 -r1.6 --- HttpServerSocket.cpp 27 Dec 2003 11:42:39 -0000 1.5 +++ HttpServerSocket.cpp 27 Dec 2003 11:56:06 -0000 1.6 @@ -113,7 +113,6 @@ * Stops the server. */ void HttpServerSocket::stop() throw (SocketException) { - ServerSocket::close(); cancel(); // make a connection for stopping accept() @@ -133,5 +132,7 @@ if(ret == -1) throw SocketException(CantCreateSockExcp, "Cannot create socket.", "HttpServerSocket::stop"); + + ServerSocket::close(); } |
From: <mik...@us...> - 2003-12-27 11:42:43
|
Update of /cvsroot/sharedaemon/sharedaemon-ui-web In directory sc8-pr-cvs1:/tmp/cvs-serv1468 Modified Files: ChangeLog Log Message: 27/12/2003 Mikael Barbeaux * Fixed a bug about rebinding sockets on the same address. Index: ChangeLog =================================================================== RCS file: /cvsroot/sharedaemon/sharedaemon-ui-web/ChangeLog,v retrieving revision 1.11 retrieving revision 1.12 diff -u -d -r1.11 -r1.12 --- ChangeLog 27 Dec 2003 10:27:10 -0000 1.11 +++ ChangeLog 27 Dec 2003 11:42:39 -0000 1.12 @@ -24,6 +24,7 @@ ----------------------------------------------------------- 27/12/2003 Mikael Barbeaux + * Fixed a bug about rebinding sockets on the same address. * Fixed a bug about accepting sockets and closing server. 26/12/2003 Mikael Barbeaux |
From: <mik...@us...> - 2003-12-27 11:42:43
|
Update of /cvsroot/sharedaemon/sharedaemon-ui-web/src/server/http In directory sc8-pr-cvs1:/tmp/cvs-serv1468/src/server/http Modified Files: HttpServerSocket.cpp HttpServerSocket.h Log Message: 27/12/2003 Mikael Barbeaux * Fixed a bug about rebinding sockets on the same address. Index: HttpServerSocket.cpp =================================================================== RCS file: /cvsroot/sharedaemon/sharedaemon-ui-web/src/server/http/HttpServerSocket.cpp,v retrieving revision 1.4 retrieving revision 1.5 diff -u -d -r1.4 -r1.5 --- HttpServerSocket.cpp 27 Dec 2003 10:55:11 -0000 1.4 +++ HttpServerSocket.cpp 27 Dec 2003 11:42:39 -0000 1.5 @@ -47,10 +47,15 @@ */ void HttpServerSocket::run() { // Starts the server - validate(); - bind(server_port); - listen(20); - + try { + validate(); + bind(server_port); + listen(20); + } + catch(Exception& e) { + cout << e.getMessage() << endl; + return; + } while(true) { // sets a cancel point setCancelPoint(); @@ -108,8 +113,8 @@ * Stops the server. */ void HttpServerSocket::stop() throw (SocketException) { + ServerSocket::close(); cancel(); - close(); // make a connection for stopping accept() int sd = socket(AF_INET, SOCK_STREAM, 0); Index: HttpServerSocket.h =================================================================== RCS file: /cvsroot/sharedaemon/sharedaemon-ui-web/src/server/http/HttpServerSocket.h,v retrieving revision 1.1 retrieving revision 1.2 diff -u -d -r1.1 -r1.2 --- HttpServerSocket.h 26 Dec 2003 22:56:41 -0000 1.1 +++ HttpServerSocket.h 27 Dec 2003 11:42:39 -0000 1.2 @@ -31,7 +31,7 @@ * A http server socket is a threaded ServerSocket * that accepts HttpSocket. */ -class HttpServerSocket : protected ServerSocket, protected Thread { +class HttpServerSocket : protected ServerSocket, public Thread { private: |
From: <mik...@us...> - 2003-12-27 11:42:42
|
Update of /cvsroot/sharedaemon/sharedaemon-ui-web/src/server/network In directory sc8-pr-cvs1:/tmp/cvs-serv1468/src/server/network Modified Files: ServerSocket.cpp Log Message: 27/12/2003 Mikael Barbeaux * Fixed a bug about rebinding sockets on the same address. Index: ServerSocket.cpp =================================================================== RCS file: /cvsroot/sharedaemon/sharedaemon-ui-web/src/server/network/ServerSocket.cpp,v retrieving revision 1.3 retrieving revision 1.4 diff -u -d -r1.3 -r1.4 --- ServerSocket.cpp 27 Dec 2003 10:27:10 -0000 1.3 +++ ServerSocket.cpp 27 Dec 2003 11:42:39 -0000 1.4 @@ -99,7 +99,7 @@ #ifdef _WIN32_ ::closesocket(server_id); #else - ::close(server_id); + cout << "close " << ::close(server_id) << endl; #endif // Change server socket state @@ -124,6 +124,11 @@ server_id = socket(AF_INET, SOCK_STREAM, 0); if(server_id == -1) throw SocketException(CantCreateSockExcp, "Cannot create server socket.", + "ServerSocket::validate"); + int t = (int) true; + int ret = setsockopt(server_id, SOL_SOCKET, SO_REUSEADDR, &t, sizeof(t)); + if(ret < 0) + throw SocketException(CantCreateSockExcp, "Cannot initialize socket option.", "ServerSocket::validate"); } |
From: <mik...@us...> - 2003-12-27 11:42:42
|
Update of /cvsroot/sharedaemon/sharedaemon-ui-web/src/test In directory sc8-pr-cvs1:/tmp/cvs-serv1468/src/test Modified Files: TestHttpSocket.cpp Log Message: 27/12/2003 Mikael Barbeaux * Fixed a bug about rebinding sockets on the same address. Index: TestHttpSocket.cpp =================================================================== RCS file: /cvsroot/sharedaemon/sharedaemon-ui-web/src/test/TestHttpSocket.cpp,v retrieving revision 1.1 retrieving revision 1.2 diff -u -d -r1.1 -r1.2 --- TestHttpSocket.cpp 26 Dec 2003 22:56:42 -0000 1.1 +++ TestHttpSocket.cpp 27 Dec 2003 11:42:39 -0000 1.2 @@ -56,15 +56,18 @@ cout << endl << endl; try { cout << "Creates a http server on port 4589..." << endl; - HttpServerSocket server(4589); + HttpServerSocket *server = new HttpServerSocket(4589); cout << "Starts the server for 30 seconds..." << endl; - server.start(); + server->start(); for(int i=0;i<20;i++) { cout << "Waited " << i << " seconds..." <<endl; Thread::sleep(1); } - cout << "Closing server..." << endl; - server.stop(); + if(server->isRunning()) { + cout << "Closing server..." << endl; + server->stop(); + } + delete server; } catch(Exception& e) { // display the exception message |
From: <mik...@us...> - 2003-12-27 10:55:15
|
Update of /cvsroot/sharedaemon/sharedaemon-ui-web/src/server/http In directory sc8-pr-cvs1:/tmp/cvs-serv27338/src/server/http Modified Files: HttpServerSocket.cpp Log Message: 27/12/2003 Mikael Barbeaux * Fixed a bug about accepting sockets and closing server. Index: HttpServerSocket.cpp =================================================================== RCS file: /cvsroot/sharedaemon/sharedaemon-ui-web/src/server/http/HttpServerSocket.cpp,v retrieving revision 1.3 retrieving revision 1.4 diff -u -d -r1.3 -r1.4 --- HttpServerSocket.cpp 27 Dec 2003 10:50:08 -0000 1.3 +++ HttpServerSocket.cpp 27 Dec 2003 10:55:11 -0000 1.4 @@ -109,6 +109,7 @@ */ void HttpServerSocket::stop() throw (SocketException) { cancel(); + close(); // make a connection for stopping accept() int sd = socket(AF_INET, SOCK_STREAM, 0); @@ -127,7 +128,5 @@ if(ret == -1) throw SocketException(CantCreateSockExcp, "Cannot create socket.", "HttpServerSocket::stop"); - - close(); } |
From: <mik...@us...> - 2003-12-27 10:50:13
|
Update of /cvsroot/sharedaemon/sharedaemon-ui-web/src/test In directory sc8-pr-cvs1:/tmp/cvs-serv26716/src/test Modified Files: TestSocket.cpp Log Message: 27/12/2003 Mikael Barbeaux * Fixed a bug about accepting sockets and closing server. Index: TestSocket.cpp =================================================================== RCS file: /cvsroot/sharedaemon/sharedaemon-ui-web/src/test/TestSocket.cpp,v retrieving revision 1.3 retrieving revision 1.4 diff -u -d -r1.3 -r1.4 --- TestSocket.cpp 27 Dec 2003 10:27:09 -0000 1.3 +++ TestSocket.cpp 27 Dec 2003 10:50:08 -0000 1.4 @@ -57,7 +57,7 @@ cout << "Validates the server socket..." << endl; server.validate(); cout << "Bind server socket on port 4589..." << endl; - server.bind(8523); + server.bind(4589); cout << "Listening for connections..." << endl; server.listen(5); cout << "Take your browser to test \"http://localhost:4589\"..." << endl; |
From: <mik...@us...> - 2003-12-27 10:50:12
|
Update of /cvsroot/sharedaemon/sharedaemon-ui-web/src/server/http In directory sc8-pr-cvs1:/tmp/cvs-serv26716/src/server/http Modified Files: HttpServerSocket.cpp Log Message: 27/12/2003 Mikael Barbeaux * Fixed a bug about accepting sockets and closing server. Index: HttpServerSocket.cpp =================================================================== RCS file: /cvsroot/sharedaemon/sharedaemon-ui-web/src/server/http/HttpServerSocket.cpp,v retrieving revision 1.2 retrieving revision 1.3 diff -u -d -r1.2 -r1.3 --- HttpServerSocket.cpp 27 Dec 2003 10:27:09 -0000 1.2 +++ HttpServerSocket.cpp 27 Dec 2003 10:50:08 -0000 1.3 @@ -115,10 +115,13 @@ if(sd == -1) throw SocketException(CantCreateSockExcp, "Cannot create socket.", "HttpServerSocket::stop"); + struct hostent *h; + h = gethostbyname("localhost"); + struct sockaddr_in address; - address.sin_family = AF_INET; - address.sin_addr.s_addr = INADDR_ANY; - address.sin_port = htons(server_port); + address.sin_family = h->h_addrtype; + memcpy((char *) &(address.sin_addr.s_addr), h->h_addr_list[0], h->h_length); + address.sin_port = htons(server_port); int ret = ::connect(sd, (struct sockaddr *) &address, sizeof(struct sockaddr_in)); if(ret == -1) |
From: <mik...@us...> - 2003-12-27 10:27:14
|
Update of /cvsroot/sharedaemon/sharedaemon-ui-web In directory sc8-pr-cvs1:/tmp/cvs-serv24081 Modified Files: ChangeLog Log Message: 27/12/2003 Mikael Barbeaux * Fixed a bug about accepting sockets and closing server. Index: ChangeLog =================================================================== RCS file: /cvsroot/sharedaemon/sharedaemon-ui-web/ChangeLog,v retrieving revision 1.10 retrieving revision 1.11 diff -u -d -r1.10 -r1.11 --- ChangeLog 26 Dec 2003 23:08:52 -0000 1.10 +++ ChangeLog 27 Dec 2003 10:27:10 -0000 1.11 @@ -23,6 +23,9 @@ ----------------------------------------------------------- +27/12/2003 Mikael Barbeaux + * Fixed a bug about accepting sockets and closing server. + 26/12/2003 Mikael Barbeaux * Updated Makefiles * Added a test program for Http sockets. |
From: <mik...@us...> - 2003-12-27 10:27:14
|
Update of /cvsroot/sharedaemon/sharedaemon-ui-web/src/server/network In directory sc8-pr-cvs1:/tmp/cvs-serv24081/src/server/network Modified Files: ServerSocket.cpp ServerSocket.h Log Message: 27/12/2003 Mikael Barbeaux * Fixed a bug about accepting sockets and closing server. Index: ServerSocket.cpp =================================================================== RCS file: /cvsroot/sharedaemon/sharedaemon-ui-web/src/server/network/ServerSocket.cpp,v retrieving revision 1.2 retrieving revision 1.3 diff -u -d -r1.2 -r1.3 --- ServerSocket.cpp 26 Dec 2003 22:56:42 -0000 1.2 +++ ServerSocket.cpp 27 Dec 2003 10:27:10 -0000 1.3 @@ -19,8 +19,6 @@ #include "ServerSocket.h" #include "../../thread/Thread.h" -#include <signal.h> -#include <sys/types.h> #ifdef _WIN32_ #define socklen_t int #endif @@ -192,10 +190,8 @@ * Throws a SocketException if an error occurs when * accepting or any other reasons ( not valid, not * bound, not listening ). - * - * @param timeout - timeout for accepting connection */ -Socket* ServerSocket::accept(int timeout) throw (SocketException) { +Socket* ServerSocket::accept() throw (SocketException) { if(server_id == -1) throw SocketException(InvalidSockExcp, "Invalid server socket.", "ServerSocket::accept"); @@ -214,25 +210,9 @@ sockaddr_in client_address; int address_length = sizeof(struct sockaddr_in); - /** - * In order to add a timeout for accepting connections ( else it will - * wait for a connection forever ), we introduce a fork process - * that will wait for the timeout to achieve, and if no client has - * connected at this time, it will kill the accept process. - */ - int pid; - if((pid = fork())) { - // father process : waiting for the timeout to achieve - Thread::sleep(timeout); - // timeout achieves : father kills his son - kill(pid, SIGKILL); - } - else { - // son process : waiting for a connection... - client_id = ::accept(server_id, (sockaddr *) &client_address, - (socklen_t *) &address_length); - } - + client_id = ::accept(server_id, (sockaddr *) &client_address, + (socklen_t *) &address_length); + if(client_id <= 0) throw SocketException(NotAcceptedSockExcp, "Received socket is invalid.", "ServerSocket::accept"); Index: ServerSocket.h =================================================================== RCS file: /cvsroot/sharedaemon/sharedaemon-ui-web/src/server/network/ServerSocket.h,v retrieving revision 1.2 retrieving revision 1.3 diff -u -d -r1.2 -r1.3 --- ServerSocket.h 26 Dec 2003 22:56:42 -0000 1.2 +++ ServerSocket.h 27 Dec 2003 10:27:10 -0000 1.3 @@ -114,11 +114,10 @@ /** * Accepts a connection. * - * @param timeout * @return Socket* - The connected socket. * @throw SocketException */ - Socket* accept(int timeout = 3) throw (SocketException); + Socket* accept() throw (SocketException); }; |
From: <mik...@us...> - 2003-12-27 10:27:13
|
Update of /cvsroot/sharedaemon/sharedaemon-ui-web/src/thread In directory sc8-pr-cvs1:/tmp/cvs-serv24081/src/thread Modified Files: Thread.h Log Message: 27/12/2003 Mikael Barbeaux * Fixed a bug about accepting sockets and closing server. Index: Thread.h =================================================================== RCS file: /cvsroot/sharedaemon/sharedaemon-ui-web/src/thread/Thread.h,v retrieving revision 1.1 retrieving revision 1.2 diff -u -d -r1.1 -r1.2 --- Thread.h 24 Dec 2003 13:03:54 -0000 1.1 +++ Thread.h 27 Dec 2003 10:27:08 -0000 1.2 @@ -114,6 +114,7 @@ */ void wait(bool toLock = true) throw (ThreadException); + }; #endif |
From: <mik...@us...> - 2003-12-27 10:27:13
|
Update of /cvsroot/sharedaemon/sharedaemon-ui-web/src/test In directory sc8-pr-cvs1:/tmp/cvs-serv24081/src/test Modified Files: TestSocket.cpp Log Message: 27/12/2003 Mikael Barbeaux * Fixed a bug about accepting sockets and closing server. Index: TestSocket.cpp =================================================================== RCS file: /cvsroot/sharedaemon/sharedaemon-ui-web/src/test/TestSocket.cpp,v retrieving revision 1.2 retrieving revision 1.3 diff -u -d -r1.2 -r1.3 --- TestSocket.cpp 26 Dec 2003 22:56:42 -0000 1.2 +++ TestSocket.cpp 27 Dec 2003 10:27:09 -0000 1.3 @@ -57,13 +57,15 @@ cout << "Validates the server socket..." << endl; server.validate(); cout << "Bind server socket on port 4589..." << endl; - server.bind(4589); + server.bind(8523); cout << "Listening for connections..." << endl; server.listen(5); cout << "Take your browser to test \"http://localhost:4589\"..." << endl; Socket *socket = server.accept(); + if(socket == 0) + cout << "socket null" << endl; cout << "Socket accepted !" << endl; - char *recv; + char *recv = new char; socket->receive(&recv); cout << "Reveiced mesage : " << endl; cout << recv << endl; |
From: <mik...@us...> - 2003-12-27 10:27:13
|
Update of /cvsroot/sharedaemon/sharedaemon-ui-web/src/server/http In directory sc8-pr-cvs1:/tmp/cvs-serv24081/src/server/http Modified Files: HttpServerSocket.cpp Log Message: 27/12/2003 Mikael Barbeaux * Fixed a bug about accepting sockets and closing server. Index: HttpServerSocket.cpp =================================================================== RCS file: /cvsroot/sharedaemon/sharedaemon-ui-web/src/server/http/HttpServerSocket.cpp,v retrieving revision 1.1 retrieving revision 1.2 diff -u -d -r1.1 -r1.2 --- HttpServerSocket.cpp 26 Dec 2003 22:56:41 -0000 1.1 +++ HttpServerSocket.cpp 27 Dec 2003 10:27:09 -0000 1.2 @@ -62,8 +62,14 @@ } catch(SocketException& se) { } - if(client == 0) + if(client == 0){ + continue; + } + if(cancel_th ==true) { + client->close(); + delete client; continue; + } // process the connection //theManager->addConnection(client); @@ -78,7 +84,7 @@ *writer << "<html><body>Request received !</body></html>"; writer->close(); HttpResponse response(writer); - response.setContentType("text/html"); cout << "ok" << endl; + response.setContentType("text/html"); client->sendHttpResponse(response); cout << "Closing socket..." << endl; client->close(); @@ -102,7 +108,23 @@ * Stops the server. */ void HttpServerSocket::stop() throw (SocketException) { - close(); cancel(); + + // make a connection for stopping accept() + int sd = socket(AF_INET, SOCK_STREAM, 0); + if(sd == -1) + throw SocketException(CantCreateSockExcp, "Cannot create socket.", + "HttpServerSocket::stop"); + struct sockaddr_in address; + address.sin_family = AF_INET; + address.sin_addr.s_addr = INADDR_ANY; + address.sin_port = htons(server_port); + int ret = ::connect(sd, (struct sockaddr *) &address, + sizeof(struct sockaddr_in)); + if(ret == -1) + throw SocketException(CantCreateSockExcp, "Cannot create socket.", + "HttpServerSocket::stop"); + + close(); } |
From: <ma...@us...> - 2003-12-27 08:39:08
|
Update of /cvsroot/sharedaemon/ui-wx/src In directory sc8-pr-cvs1:/tmp/cvs-serv10573 Modified Files: Images.cpp Images.h MBitmapButton.cpp MBitmapButton.h Log Message: wxGTK/wxMac version of MBitmapButton now also updates itself correctly on iconset change. Index: Images.cpp =================================================================== RCS file: /cvsroot/sharedaemon/ui-wx/src/Images.cpp,v retrieving revision 1.43 retrieving revision 1.44 diff -u -d -r1.43 -r1.44 --- Images.cpp 25 Dec 2003 02:18:57 -0000 1.43 +++ Images.cpp 27 Dec 2003 08:39:04 -0000 1.44 @@ -273,6 +273,9 @@ GetImage(parent->GetName()) ); } + if (parent->GetClassInfo()->IsKindOf(CLASSINFO(MBitmapButton))) { + ((MBitmapButton*)parent)->GenImage(); + } wxWindowList controls; controls = parent->GetChildren(); for (unsigned int i=0;i<controls.GetCount();i++) { Index: Images.h =================================================================== RCS file: /cvsroot/sharedaemon/ui-wx/src/Images.h,v retrieving revision 1.20 retrieving revision 1.21 diff -u -d -r1.20 -r1.21 --- Images.h 25 Dec 2003 02:18:57 -0000 1.20 +++ Images.h 27 Dec 2003 08:39:04 -0000 1.21 @@ -29,6 +29,7 @@ #endif #include "defines.h" +#include "MBitmapButton.h" WX_DECLARE_LIST(wxBitmap, ListOfBitmaps); Index: MBitmapButton.cpp =================================================================== RCS file: /cvsroot/sharedaemon/ui-wx/src/MBitmapButton.cpp,v retrieving revision 1.9 retrieving revision 1.10 diff -u -d -r1.9 -r1.10 --- MBitmapButton.cpp 27 Dec 2003 07:18:20 -0000 1.9 +++ MBitmapButton.cpp 27 Dec 2003 08:39:04 -0000 1.10 @@ -87,15 +87,16 @@ } #else /* Use wxBitmapButton on other platforms */ + +IMPLEMENT_DYNAMIC_CLASS(MBitmapButton, wxBitmapButton) + BEGIN_EVENT_TABLE(MBitmapButton, wxBitmapButton) EVT_BUTTON(-1, MBitmapButton::OnClick) EVT_SIZE(MBitmapButton::OnSize) END_EVENT_TABLE() /************************************************************** MBitmapButton */ -/* This method constructs a bitmapbutton control from the passed arguments by */ -/* creating a new wxBitmap in memoryDC, drawing the passed name/image in it, */ -/* and setting it as BitmapLabel. */ +/* Constructor - stores the neccesery variables locally and calls GenImage(). */ /* @parent Which wxWindow shall be this controls parent */ /* @id Identification code (int) for this control */ /* @name Name label to be shown on this button */ @@ -105,19 +106,66 @@ /* @style Style flag - see wxWindow/wxControl style flags in wxHelp */ /******************************************************************************/ MBitmapButton::MBitmapButton( - wxWindow *parent, wxWindowID id, const wxString &name, const wxBitmap + wxWindow *parent, wxWindowID id, const wxString &name, const wxString &image, const wxPoint &pos, const wxSize &size, long style, const wxValidator &val) : wxBitmapButton(parent, id, wxNullBitmap, pos, size, style, val, name) { + m_name = name; + m_image = image; + GenImage(); +} + +/********************************************************************* OnSize */ +/* This method creates a new temporary bitmap with new control size, copies */ +/* the currently set bitmap into the temporary bitmap and sets the new image */ +/* as BitmapLabel. This is done to preserve the label left-alignment even */ +/* after size change. */ +/******************************************************************************/ +void MBitmapButton::OnSize(wxSizeEvent &event) { +wxMemoryDC mdc; + + wxBitmap tmp( + GetSize().GetWidth()+LABEL_POS_OFFSET, + GetSize().GetHeight()+LABEL_POS_OFFSET + ); + mdc.SelectObject(tmp); + +#ifdef __WXMSW__ /* This gives transparent image on wxMSW */ + mdc.SetPen(*wxTRANSPARENT_PEN); + mdc.SetBrush(wxBrush(*wxLIGHT_GREY, wxSOLID)); +#else /* Elsewhere, it will be done with wxMask later */ + mdc.SetBrush(wxBrush(wxColour(0, 0, 0), wxSOLID)); +#endif + mdc.DrawRectangle(0, 0, tmp.GetWidth(), tmp.GetHeight()); + mdc.DrawBitmap(orig_img, 0, 0, true); +#ifndef __WXMSW__ + tmp.SetMask(new wxMask(tmp, wxColour(0, 0, 0))); +#endif + SetBitmapLabel(tmp); + + event.Skip(); +} + +/******************************************************************* GenImage */ +/* This method generates the bitmap label for this control. We create a */ +/* temporary bitmap for measuring font length, and then create final image */ +/* with correct calculated size. Finally, draw the image and text there. */ +/* Rectangle drawing and various #ifdefs are required to achieve correct */ +/* transparency on all platforms. */ +/******************************************************************************/ +void MBitmapButton::GenImage() { wxMemoryDC mdc; wxBitmap *tmp; wxCoord x, y; + + wxBitmap bitmap(img->GetImage(m_image)); + mdc.SelectObject(wxBitmap(100, 100)); mdc.SetFont(wxSystemSettings::GetFont(wxSYS_SYSTEM_FONT)); - mdc.GetTextExtent(name, &x, &y); - x+=image.GetWidth()+LABEL_WIDTH_ADDITION; + mdc.GetTextExtent(m_name, &x, &y); + x+=bitmap.GetWidth()+LABEL_WIDTH_ADDITION; - image.GetHeight() > y ? y = image.GetHeight()+5 : y+=5; + bitmap.GetHeight() > y ? y = bitmap.GetHeight()+5 : y+=5; tmp = new wxBitmap(x, y); @@ -131,12 +179,12 @@ mdc.DrawRectangle(0, 0, tmp->GetWidth(), tmp->GetHeight()); mdc.DrawBitmap( - image, 5, ((y-image.GetHeight())/2)+BITMAP_Y_POS_OFFSET, true + bitmap, 5, ((y-bitmap.GetHeight())/2)+BITMAP_Y_POS_OFFSET, true ); mdc.SetTextForeground(wxColour(50, 50, 50)); mdc.DrawText( - name, image.GetWidth()+10, + m_name, bitmap.GetWidth()+10, ((tmp->GetHeight()-GetCharHeight())/2)+TEXT_Y_POS_OFFSET ); @@ -147,37 +195,6 @@ orig_img = *tmp; SetSize(wxSize(tmp->GetWidth()+10, tmp->GetHeight()+5)); delete tmp; -} - -/********************************************************************* OnSize */ -/* This method creates a new temporary bitmap with new control size, copies */ -/* the currently set bitmap into the temporary bitmap and sets the new image */ -/* as BitmapLabel. This is done to preserve the label left-alignment even */ -/* after size change. */ -/******************************************************************************/ -void MBitmapButton::OnSize(wxSizeEvent &event) { -wxMemoryDC mdc; - - wxBitmap tmp( - GetSize().GetWidth()+LABEL_POS_OFFSET, - GetSize().GetHeight()+LABEL_POS_OFFSET - ); - mdc.SelectObject(tmp); - -#ifdef __WXMSW__ /* This gives transparent image on wxMSW */ - mdc.SetPen(*wxTRANSPARENT_PEN); - mdc.SetBrush(wxBrush(*wxLIGHT_GREY, wxSOLID)); -#else /* Elsewhere, it will be done with wxMask later */ - mdc.SetBrush(wxBrush(wxColour(0, 0, 0), wxSOLID)); -#endif - mdc.DrawRectangle(0, 0, tmp.GetWidth(), tmp.GetHeight()); - mdc.DrawBitmap(orig_img, 0, 0, true); -#ifndef __WXMSW__ - tmp.SetMask(new wxMask(tmp, wxColour(0, 0, 0))); -#endif - SetBitmapLabel(tmp); - - event.Skip(); } #endif Index: MBitmapButton.h =================================================================== RCS file: /cvsroot/sharedaemon/ui-wx/src/MBitmapButton.h,v retrieving revision 1.7 retrieving revision 1.8 diff -u -d -r1.7 -r1.8 --- MBitmapButton.h 27 Dec 2003 07:18:20 -0000 1.7 +++ MBitmapButton.h 27 Dec 2003 08:39:04 -0000 1.8 @@ -94,18 +94,22 @@ /* passed to us and setting it as this controls BitmapLabel. */ /******************************************************************************/ class MBitmapButton : public wxBitmapButton { +DECLARE_DYNAMIC_CLASS(MBitmapButton) public: MBitmapButton( /* Constructor */ wxWindow *parent, wxWindowID id, const wxString &title, const - wxBitmap &image, const wxPoint &pos = wxDefaultPosition, + wxString &image, const wxPoint &pos = wxDefaultPosition, const wxSize &size = wxDefaultSize, long style = wxBU_AUTODRAW, const wxValidator &val = wxDefaultValidator ); + MBitmapButton() {} /* Default constructor */ + void GenImage(); /* Generates the image for this control */ private: DECLARE_EVENT_TABLE() void OnClick(wxCommandEvent &event){ event.Skip(); } /* Skips events */ void OnSize(wxSizeEvent &event); /* Updates image */ wxBitmap orig_img; /* Original generated image */ + wxString m_name, m_image; /* Name and image of the control */ }; #endif // ___WXMSW__ |
From: <ma...@us...> - 2003-12-27 07:39:57
|
Update of /cvsroot/sharedaemon/ui-wx/src In directory sc8-pr-cvs1:/tmp/cvs-serv3253/src Modified Files: GUISettingsDlg.cpp MainDlg.cpp Log Message: Fixed toolbar images updating after iconset change. Removed mainframe flickering during iconset changing. Index: GUISettingsDlg.cpp =================================================================== RCS file: /cvsroot/sharedaemon/ui-wx/src/GUISettingsDlg.cpp,v retrieving revision 1.31 retrieving revision 1.32 diff -u -d -r1.31 -r1.32 --- GUISettingsDlg.cpp 26 Dec 2003 11:36:32 -0000 1.31 +++ GUISettingsDlg.cpp 27 Dec 2003 07:39:54 -0000 1.32 @@ -245,18 +245,15 @@ /* If iconset setting was changed update the images in gui */ if (old_icon_set != icon_set) { - /* Reload images */ + mainframe->Freeze(); + img->LoadImages(); - /* Update static bitmaps */ img->UpdateImages(mainframe); - /* Recreate toolbar */ mainframe->CreateMyToolBar(true); - /* Update server page logbook */ serverwnd->SetLogBookImages(); - /* Update statistics tree */ statisticswnd->GetTree()->GetTreeImages(); statisticswnd->GetTree()->GenerateTree(); - /* Main frame and systray icons */ + wxIcon icon; icon.CopyFromBitmap(img->GetImage(wxT("mule"))); mainframe->SetIcon(icon); @@ -265,6 +262,8 @@ systray->SetIcon(icon); #endif #endif + + mainframe->Thaw(); } } Index: MainDlg.cpp =================================================================== RCS file: /cvsroot/sharedaemon/ui-wx/src/MainDlg.cpp,v retrieving revision 1.41 retrieving revision 1.42 diff -u -d -r1.41 -r1.42 --- MainDlg.cpp 27 Dec 2003 07:18:20 -0000 1.41 +++ MainDlg.cpp 27 Dec 2003 07:39:54 -0000 1.42 @@ -315,7 +315,7 @@ tool_align|wxNO_BORDER|wxTB_3DBUTTONS|wxTB_FLAT|wxCLIP_CHILDREN ); - if (start_up) { + if (start_up || gen_images) { wxArrayString names; for (i=0;i<pages.GetCount();i++) { if (pages.IsEmpty()) { |
From: <ma...@us...> - 2003-12-27 07:39:57
|
Update of /cvsroot/sharedaemon/ui-wx In directory sc8-pr-cvs1:/tmp/cvs-serv3253 Modified Files: Changelog Log Message: Fixed toolbar images updating after iconset change. Removed mainframe flickering during iconset changing. Index: Changelog =================================================================== RCS file: /cvsroot/sharedaemon/ui-wx/Changelog,v retrieving revision 1.115 retrieving revision 1.116 diff -u -d -r1.115 -r1.116 --- Changelog 26 Dec 2003 14:27:35 -0000 1.115 +++ Changelog 27 Dec 2003 07:39:53 -0000 1.116 @@ -25,6 +25,11 @@ # This also helps in backtracking changes, or reviewing development history. # ############################################################################### +2003/12/27 Alo Sarv + * New bitmapbuttons are new iconset-theme aware. + * Fixed toolbar images updating on iconset changing. + * Removed main frame flickering during iconset changing. + 2003/12/26 Alo Sarv * Fixed configure script to be compatible with BSD. * Fixed the i18n issues that arose after implementing dynamic pages |
Update of /cvsroot/sharedaemon/ui-wx/src In directory sc8-pr-cvs1:/tmp/cvs-serv1039 Modified Files: MBitmapButton.cpp MBitmapButton.h MMultiButton.cpp MMultiButton.h MainDlg.cpp SBPanel.cpp SBPanel.h ServerWnd.cpp Log Message: New bitmapbuttons are now iconset-theme aware. Index: MBitmapButton.cpp =================================================================== RCS file: /cvsroot/sharedaemon/ui-wx/src/MBitmapButton.cpp,v retrieving revision 1.8 retrieving revision 1.9 diff -u -d -r1.8 -r1.9 --- MBitmapButton.cpp 25 Dec 2003 02:18:57 -0000 1.8 +++ MBitmapButton.cpp 27 Dec 2003 07:18:20 -0000 1.9 @@ -42,22 +42,21 @@ /* @id Identification code (int) for this control */ /* @name Name label to be shown on this button */ /* @image Image label to be shown on this button */ +/* @image_name Name of the StaticBitmap - needed for iconset changing engine */ /* @pos Position to insert this control to */ /* @size Size of this control */ /* @style Style flag - see wxWindow/wxControl style flags in wxHelp */ /******************************************************************************/ MBitmapButton::MBitmapButton( - wxWindow *parent, wxWindowID id, const wxString &name, const wxBitmap + wxWindow *parent, wxWindowID id, const wxString &name, const wxString &image, const wxPoint &pos, const wxSize &size, long style ) : wxButton(parent, id, name, pos, size, style) { - - wxString imgname; - imgname.Format(wxT("%simg_%s"), imgname.c_str(), name.c_str()); + wxBitmap bitmap = img->GetImage(image); wxFlexGridSizer *s_main = new wxFlexGridSizer(0, 0); wxStaticBitmap *img = new wxStaticBitmap( - this, 1000, image, wxDefaultPosition, - wxSize(image.GetWidth(), image.GetHeight()), 5, imgname + this, 1000, bitmap, wxDefaultPosition, + wxSize(bitmap.GetWidth(), bitmap.GetHeight()), 5, image ); s_main->Add(img, 0, wxALL, 5); Index: MBitmapButton.h =================================================================== RCS file: /cvsroot/sharedaemon/ui-wx/src/MBitmapButton.h,v retrieving revision 1.6 retrieving revision 1.7 diff -u -d -r1.6 -r1.7 --- MBitmapButton.h 25 Dec 2003 02:18:57 -0000 1.6 +++ MBitmapButton.h 27 Dec 2003 07:18:20 -0000 1.7 @@ -28,6 +28,8 @@ #include "wx/wx.h" #endif +#include "Images.h" + /* Image/Text positioning/sizing constants */ /* These constants adjust the positioning and sizing of this buttons bitmap */ /* label. Since all platforms have different "base" button sizes/borders etc, */ @@ -76,8 +78,8 @@ public: MBitmapButton( /* Constructor */ wxWindow *parent, wxWindowID id, const wxString &title, - const wxBitmap &image, const wxPoint &pos = wxDefaultPosition, - const wxSize &size = wxDefaultSize, long style = + const wxString &image, const wxPoint &pos = wxDefaultPosition, + const wxSize &size = wxDefaultSize, long style = wxNO_FULL_REPAINT_ON_RESIZE ); private: Index: MMultiButton.cpp =================================================================== RCS file: /cvsroot/sharedaemon/ui-wx/src/MMultiButton.cpp,v retrieving revision 1.4 retrieving revision 1.5 diff -u -d -r1.4 -r1.5 --- MMultiButton.cpp 23 Dec 2003 00:59:20 -0000 1.4 +++ MMultiButton.cpp 27 Dec 2003 07:18:20 -0000 1.5 @@ -30,9 +30,6 @@ #include "MMultiButton.h" -#include <wx/listimpl.cpp> -WX_DEFINE_LIST(MBitmapList); - BEGIN_EVENT_TABLE(MMultiButton, wxControl) EVT_PAINT(MMultiButton::OnPaint) END_EVENT_TABLE() @@ -88,8 +85,8 @@ /******************************************************************************/ MMultiButton::MMultiButton( wxWindow *parent, wxWindowID id, size_t count, const wxArrayString - &names, int ids[], const MBitmapList &images, const wxPoint &pos, const - wxSize &size, long style + &names, int ids[], const wxArrayString &images, const wxPoint &pos, + const wxSize &size, long style ) : wxControl(parent, id, pos, size, style) { MBitmapButton *btn; @@ -100,7 +97,7 @@ s_main->AddGrowableCol(0); for (size_t i=0;i<count;i++) { btn = new MBitmapButton( - this, ids[i], names[i], *(images.Item(i)->GetData()), + this, ids[i], names[i], images[i], wxDefaultPosition, wxDefaultSize, style ); s_main->Add(btn, 0, wxGROW, 0); Index: MMultiButton.h =================================================================== RCS file: /cvsroot/sharedaemon/ui-wx/src/MMultiButton.h,v retrieving revision 1.1 retrieving revision 1.2 diff -u -d -r1.1 -r1.2 --- MMultiButton.h 22 Dec 2003 05:04:15 -0000 1.1 +++ MMultiButton.h 27 Dec 2003 07:18:20 -0000 1.2 @@ -30,8 +30,6 @@ #include "MBitmapButton.h" -WX_DECLARE_LIST(wxBitmap, MBitmapList); - /*************************************************************** MMultiButton */ /* MMultiButton is a control with multiple buttons. The buttons can either be */ /* normal buttons, or MBitmapButtons. */ @@ -46,7 +44,7 @@ ); MMultiButton( /* Constructs a multi-button with MBitmapButtons. */ wxWindow *parent, wxWindowID id, size_t count, const - wxArrayString &names, int ids[], const MBitmapList &images, + wxArrayString &names, int ids[], const wxArrayString &images, const wxPoint &pos = wxDefaultPosition, const wxSize &size = wxDefaultSize, long style = wxNO_BORDER| wxNO_FULL_REPAINT_ON_RESIZE Index: MainDlg.cpp =================================================================== RCS file: /cvsroot/sharedaemon/ui-wx/src/MainDlg.cpp,v retrieving revision 1.40 retrieving revision 1.41 diff -u -d -r1.40 -r1.41 --- MainDlg.cpp 26 Dec 2003 11:36:32 -0000 1.40 +++ MainDlg.cpp 27 Dec 2003 07:18:20 -0000 1.41 @@ -293,11 +293,9 @@ bool show_tool; /* For storing values read from configuration */ unsigned int i; /* Loop counter */ wxArrayString names; /* Names for multibutton controls */ -MBitmapList images; /* Images for multibutton controls */ +wxArrayString images; /* Images for multibutton controls */ int ids[2]; /* Identifiers for multibutton controls */ - images.DeleteContents(true); - MyToolBar *toolbar = (MyToolBar*)GetToolBar(); delete toolbar; SetToolBar(NULL); @@ -337,11 +335,11 @@ names.Add(_("Log")); ids[1] = ID_BTN_CONNECT; ids[1] = ID_BTN_LOG; - images.Append(new wxBitmap(img->GetImage(wxT("connection")))); - images.Append(new wxBitmap(img->GetImage(wxT("log")))); + images.Add(wxT("connection")); + images.Add(wxT("log")); tb->AddControl(new MMultiButton(tb, -1, 2, names, ids, images)); images.Clear(); - names.Empty(); + names.Clear(); for (i=0;i<pages.GetCount();i++) { if (pages.IsEmpty()){ @@ -368,11 +366,11 @@ names.Add(_("Help")); ids[0] = ID_BTN_GUISETTINGS; ids[1] = ID_BTN_HELP; - images.Append(new wxBitmap(img->GetImage(wxT("options")))); - images.Append(new wxBitmap(img->GetImage(wxT("help")))); + images.Add(wxT("options")); + images.Add(wxT("help")); tb->AddControl(new MMultiButton(tb, -1, 2, names, ids, images)); images.Clear(); - names.Empty(); + names.Clear(); tb->Realize(); SetToolBar(tb); Index: SBPanel.cpp =================================================================== RCS file: /cvsroot/sharedaemon/ui-wx/src/SBPanel.cpp,v retrieving revision 1.15 retrieving revision 1.16 diff -u -d -r1.15 -r1.16 --- SBPanel.cpp 25 Dec 2003 07:02:56 -0000 1.15 +++ SBPanel.cpp 27 Dec 2003 07:18:20 -0000 1.16 @@ -43,12 +43,12 @@ */ CSBPanel::CSBPanel( wxWindow *parent, wxWindowID id, const wxString &title, - const wxString &name, const wxBitmap &image + const wxString &name, const wxString &image ) : wxPanel( parent, id, wxDefaultPosition, wxDefaultSize, wxTAB_TRAVERSAL, name) { - if (image != wxNullBitmap) { + if (image != wxEmptyString) { use_image = true; } else { use_image = false; Index: SBPanel.h =================================================================== RCS file: /cvsroot/sharedaemon/ui-wx/src/SBPanel.h,v retrieving revision 1.6 retrieving revision 1.7 diff -u -d -r1.6 -r1.7 --- SBPanel.h 22 Dec 2003 05:36:15 -0000 1.6 +++ SBPanel.h 27 Dec 2003 07:18:20 -0000 1.7 @@ -54,7 +54,7 @@ CSBPanel( wxWindow *parent, wxWindowID id, const wxString &title, const wxString &name, - const wxBitmap &image = wxNullBitmap + const wxString &image = wxEmptyString ); virtual ~CSBPanel(); Index: ServerWnd.cpp =================================================================== RCS file: /cvsroot/sharedaemon/ui-wx/src/ServerWnd.cpp,v retrieving revision 1.51 retrieving revision 1.52 diff -u -d -r1.51 -r1.52 --- ServerWnd.cpp 25 Dec 2003 07:22:10 -0000 1.51 +++ ServerWnd.cpp 27 Dec 2003 07:18:20 -0000 1.52 @@ -191,7 +191,7 @@ /* Sidebar sections */ CSBPanel *addserver = new CSBPanel( sidebar, -1, _("Add new server"), wxT("SideBar_AddServer"), - img->GetImage(wxT("newserver")) + wxT("newserver") ); addserver->SetContent(AddServerPanel(addserver, false, false)); sbmain->Add(addserver, 0, wxGROW|wxALL|wxADJUST_MINSIZE, 0); @@ -199,21 +199,21 @@ CSBPanel *serversettings = new CSBPanel( sidebar, -1, _("Server settings"), wxT("SideBar_ServerSettings"), - img->GetImage(wxT("updateservers")) + wxT("updateservers") ); serversettings->SetContent(SettingsPanel(serversettings, false, false)); sbmain->Add(serversettings, 0, wxGROW|wxALL|wxADJUST_MINSIZE, 0); CSBPanel *portsettings = new CSBPanel( sidebar, -1, _("Port settings"), wxT("SideBar_PortSettings"), - img->GetImage(wxT("server")) + wxT("server") ); portsettings->SetContent(PortSettingsPanel(portsettings, false, false)); sbmain->Add(portsettings, 0, wxGROW|wxALL|wxADJUST_MINSIZE, 0); CSBPanel *logoptions = new CSBPanel( sidebar, -1, _("Log options"), wxT("SideBar_LoggingOptions"), - img->GetImage(wxT("log")) + wxT("log") ); logoptions->SetContent(LogOptionsPanel(logoptions, false, false)); sbmain->Add(logoptions, 0, wxGROW|wxALL|wxADJUST_MINSIZE, 0); |
From: <mik...@us...> - 2003-12-26 23:08:55
|
Update of /cvsroot/sharedaemon/sharedaemon-ui-web In directory sc8-pr-cvs1:/tmp/cvs-serv5756 Modified Files: Makefile.bsd Makefile.osx Makefile.win Makefile.lin ChangeLog Log Message: 26/12/2003 Mikael Barbeaux * Updated Makefiles Index: Makefile.bsd =================================================================== RCS file: /cvsroot/sharedaemon/sharedaemon-ui-web/Makefile.bsd,v retrieving revision 1.8 retrieving revision 1.9 diff -u -d -r1.8 -r1.9 --- Makefile.bsd 26 Dec 2003 22:59:38 -0000 1.8 +++ Makefile.bsd 26 Dec 2003 23:08:52 -0000 1.9 @@ -31,6 +31,8 @@ OBJECTS = src/exceptions/Exception.o \ src/exceptions/ThreadException.o \ src/exceptions/SocketException.o \ + src/exceptions/HttpException.o \ + src/exceptions/WriterException.o \ src/server/network/Socket.o \ src/server/network/ServerSocket.o \ src/server/http/HttpConstants.o \ Index: Makefile.osx =================================================================== RCS file: /cvsroot/sharedaemon/sharedaemon-ui-web/Makefile.osx,v retrieving revision 1.8 retrieving revision 1.9 diff -u -d -r1.8 -r1.9 --- Makefile.osx 26 Dec 2003 22:59:38 -0000 1.8 +++ Makefile.osx 26 Dec 2003 23:08:52 -0000 1.9 @@ -31,6 +31,8 @@ OBJECTS = src/exceptions/Exception.o \ src/exceptions/ThreadException.o \ src/exceptions/SocketException.o \ + src/exceptions/HttpException.o \ + src/exceptions/WriterException.o \ src/server/network/Socket.o \ src/server/network/ServerSocket.o \ src/server/http/HttpConstants.o \ Index: Makefile.win =================================================================== RCS file: /cvsroot/sharedaemon/sharedaemon-ui-web/Makefile.win,v retrieving revision 1.9 retrieving revision 1.10 diff -u -d -r1.9 -r1.10 --- Makefile.win 26 Dec 2003 22:59:38 -0000 1.9 +++ Makefile.win 26 Dec 2003 23:08:52 -0000 1.10 @@ -37,6 +37,8 @@ OBJECTS = src/exceptions/Exception.o \ src/exceptions/ThreadException.o \ src/exceptions/SocketException.o \ + src/exceptions/HttpException.o \ + src/exceptions/WriterException.o \ src/server/network/Socket.o \ src/server/network/ServerSocket.o \ src/server/http/HttpConstants.o \ Index: Makefile.lin =================================================================== RCS file: /cvsroot/sharedaemon/sharedaemon-ui-web/Makefile.lin,v retrieving revision 1.8 retrieving revision 1.9 diff -u -d -r1.8 -r1.9 --- Makefile.lin 26 Dec 2003 22:59:38 -0000 1.8 +++ Makefile.lin 26 Dec 2003 23:08:52 -0000 1.9 @@ -30,6 +30,8 @@ OBJECTS = src/exceptions/Exception.o \ src/exceptions/ThreadException.o \ src/exceptions/SocketException.o \ + src/exceptions/HttpException.o \ + src/exceptions/WriterException.o \ src/server/network/Socket.o \ src/server/network/ServerSocket.o \ src/server/http/HttpConstants.o \ Index: ChangeLog =================================================================== RCS file: /cvsroot/sharedaemon/sharedaemon-ui-web/ChangeLog,v retrieving revision 1.9 retrieving revision 1.10 diff -u -d -r1.9 -r1.10 --- ChangeLog 26 Dec 2003 22:56:43 -0000 1.9 +++ ChangeLog 26 Dec 2003 23:08:52 -0000 1.10 @@ -24,6 +24,7 @@ ----------------------------------------------------------- 26/12/2003 Mikael Barbeaux + * Updated Makefiles * Added a test program for Http sockets. * Fixed a problem about accepting sockets which were an infinite process. * Implemented main http controls : HttpRequest, HttpResponse, HttpSocket, |
From: <mik...@us...> - 2003-12-26 22:59:42
|
Update of /cvsroot/sharedaemon/sharedaemon-ui-web In directory sc8-pr-cvs1:/tmp/cvs-serv4305 Modified Files: Makefile.bsd Makefile.osx Makefile.win Makefile.lin Log Message: 26/12/2003 Mikael Barbeaux * Added a test program for Http sockets. Index: Makefile.bsd =================================================================== RCS file: /cvsroot/sharedaemon/sharedaemon-ui-web/Makefile.bsd,v retrieving revision 1.7 retrieving revision 1.8 diff -u -d -r1.7 -r1.8 --- Makefile.bsd 26 Dec 2003 22:56:43 -0000 1.7 +++ Makefile.bsd 26 Dec 2003 22:59:38 -0000 1.8 @@ -44,6 +44,7 @@ src/test/TestThread.o \ src/test/TestSemaphore.o \ src/test/TestSocket.o \ + src/test/TestHttpSocket.o \ src/thread/Mutex.o \ src/thread/Condition.o \ src/thread/Semaphore.o \ Index: Makefile.osx =================================================================== RCS file: /cvsroot/sharedaemon/sharedaemon-ui-web/Makefile.osx,v retrieving revision 1.7 retrieving revision 1.8 diff -u -d -r1.7 -r1.8 --- Makefile.osx 26 Dec 2003 22:56:43 -0000 1.7 +++ Makefile.osx 26 Dec 2003 22:59:38 -0000 1.8 @@ -44,6 +44,7 @@ src/test/TestThread.o \ src/test/TestSemaphore.o \ src/test/TestSocket.o \ + src/test/TestHttpSocket.o \ src/thread/Mutex.o \ src/thread/Condition.o \ src/thread/Semaphore.o \ Index: Makefile.win =================================================================== RCS file: /cvsroot/sharedaemon/sharedaemon-ui-web/Makefile.win,v retrieving revision 1.8 retrieving revision 1.9 diff -u -d -r1.8 -r1.9 --- Makefile.win 26 Dec 2003 22:56:43 -0000 1.8 +++ Makefile.win 26 Dec 2003 22:59:38 -0000 1.9 @@ -50,6 +50,7 @@ src/test/TestThread.o \ src/test/TestSemaphore.o \ src/test/TestSocket.o \ + src/test/TestHttpSocket.o \ src/thread/Mutex.o \ src/thread/Condition.o \ src/thread/Semaphore.o \ Index: Makefile.lin =================================================================== RCS file: /cvsroot/sharedaemon/sharedaemon-ui-web/Makefile.lin,v retrieving revision 1.7 retrieving revision 1.8 diff -u -d -r1.7 -r1.8 --- Makefile.lin 26 Dec 2003 22:56:43 -0000 1.7 +++ Makefile.lin 26 Dec 2003 22:59:38 -0000 1.8 @@ -43,6 +43,7 @@ src/test/TestThread.o \ src/test/TestSemaphore.o \ src/test/TestSocket.o \ + src/test/TestHttpSocket.o \ src/thread/Mutex.o \ src/thread/Condition.o \ src/thread/Semaphore.o \ |
From: <mik...@us...> - 2003-12-26 22:56:47
|
Update of /cvsroot/sharedaemon/sharedaemon-ui-web/src/server/network In directory sc8-pr-cvs1:/tmp/cvs-serv3848/src/server/network Modified Files: ServerSocket.cpp ServerSocket.h Log Message: 26/12/2003 Mikael Barbeaux * Added a test program for Http sockets. * Fixed a problem about accepting sockets which were an infinite process. * Implemented main http controls : HttpRequest, HttpResponse, HttpSocket, HttpServerSocket and Writers Index: ServerSocket.cpp =================================================================== RCS file: /cvsroot/sharedaemon/sharedaemon-ui-web/src/server/network/ServerSocket.cpp,v retrieving revision 1.1 retrieving revision 1.2 diff -u -d -r1.1 -r1.2 --- ServerSocket.cpp 26 Dec 2003 10:04:51 -0000 1.1 +++ ServerSocket.cpp 26 Dec 2003 22:56:42 -0000 1.2 @@ -18,9 +18,14 @@ */ #include "ServerSocket.h" +#include "../../thread/Thread.h" +#include <signal.h> +#include <sys/types.h> #ifdef _WIN32_ #define socklen_t int #endif +#include <iostream> +using namespace std; /** * Creates a generic ServerSocket. @@ -101,7 +106,6 @@ // Change server socket state closed = true; - } /** @@ -188,8 +192,10 @@ * Throws a SocketException if an error occurs when * accepting or any other reasons ( not valid, not * bound, not listening ). - */ -Socket* ServerSocket::accept() throw (SocketException) { + * + * @param timeout - timeout for accepting connection + */ +Socket* ServerSocket::accept(int timeout) throw (SocketException) { if(server_id == -1) throw SocketException(InvalidSockExcp, "Invalid server socket.", "ServerSocket::accept"); @@ -204,16 +210,31 @@ "Server socket isn't listening for connections", "ServerSocket::accept"); // Client informations - int client_id; + int client_id = -1; sockaddr_in client_address; int address_length = sizeof(struct sockaddr_in); - // Accepting connection - client_id = ::accept(server_id, (sockaddr *) &client_address, + /** + * In order to add a timeout for accepting connections ( else it will + * wait for a connection forever ), we introduce a fork process + * that will wait for the timeout to achieve, and if no client has + * connected at this time, it will kill the accept process. + */ + int pid; + if((pid = fork())) { + // father process : waiting for the timeout to achieve + Thread::sleep(timeout); + // timeout achieves : father kills his son + kill(pid, SIGKILL); + } + else { + // son process : waiting for a connection... + client_id = ::accept(server_id, (sockaddr *) &client_address, (socklen_t *) &address_length); - + } + if(client_id <= 0) - throw SocketException(InvalidSockExcp, "Received socket is invalid.", + throw SocketException(NotAcceptedSockExcp, "Received socket is invalid.", "ServerSocket::accept"); // Create socket for this connection Index: ServerSocket.h =================================================================== RCS file: /cvsroot/sharedaemon/sharedaemon-ui-web/src/server/network/ServerSocket.h,v retrieving revision 1.1 retrieving revision 1.2 diff -u -d -r1.1 -r1.2 --- ServerSocket.h 26 Dec 2003 10:04:51 -0000 1.1 +++ ServerSocket.h 26 Dec 2003 22:56:42 -0000 1.2 @@ -36,7 +36,7 @@ */ class ServerSocket { - private: + protected: // address of this server socket sockaddr_in server_address; @@ -114,10 +114,11 @@ /** * Accepts a connection. * + * @param timeout * @return Socket* - The connected socket. * @throw SocketException */ - Socket* accept() throw (SocketException); + Socket* accept(int timeout = 3) throw (SocketException); }; |
From: <mik...@us...> - 2003-12-26 22:56:47
|
Update of /cvsroot/sharedaemon/sharedaemon-ui-web In directory sc8-pr-cvs1:/tmp/cvs-serv3848 Modified Files: Makefile.lin Makefile.win Makefile.bsd Makefile.osx ChangeLog Log Message: 26/12/2003 Mikael Barbeaux * Added a test program for Http sockets. * Fixed a problem about accepting sockets which were an infinite process. * Implemented main http controls : HttpRequest, HttpResponse, HttpSocket, HttpServerSocket and Writers Index: Makefile.lin =================================================================== RCS file: /cvsroot/sharedaemon/sharedaemon-ui-web/Makefile.lin,v retrieving revision 1.6 retrieving revision 1.7 diff -u -d -r1.6 -r1.7 --- Makefile.lin 26 Dec 2003 10:07:58 -0000 1.6 +++ Makefile.lin 26 Dec 2003 22:56:43 -0000 1.7 @@ -32,6 +32,12 @@ src/exceptions/SocketException.o \ src/server/network/Socket.o \ src/server/network/ServerSocket.o \ + src/server/http/HttpConstants.o \ + src/server/http/HttpRequest.o \ + src/server/http/HttpResponse.o \ + src/server/http/HttpSocket.o \ + src/server/http/HttpServerSocket.o \ + src/server/http/TextWriter.o \ src/test/TestHandler.o \ src/test/TestException.o \ src/test/TestThread.o \ Index: Makefile.win =================================================================== RCS file: /cvsroot/sharedaemon/sharedaemon-ui-web/Makefile.win,v retrieving revision 1.7 retrieving revision 1.8 diff -u -d -r1.7 -r1.8 --- Makefile.win 26 Dec 2003 10:31:16 -0000 1.7 +++ Makefile.win 26 Dec 2003 22:56:43 -0000 1.8 @@ -39,6 +39,12 @@ src/exceptions/SocketException.o \ src/server/network/Socket.o \ src/server/network/ServerSocket.o \ + src/server/http/HttpConstants.o \ + src/server/http/HttpRequest.o \ + src/server/http/HttpResponse.o \ + src/server/http/HttpSocket.o \ + src/server/http/HttpServerSocket.o \ + src/server/http/TextWriter.o \ src/test/TestHandler.o \ src/test/TestException.o \ src/test/TestThread.o \ Index: Makefile.bsd =================================================================== RCS file: /cvsroot/sharedaemon/sharedaemon-ui-web/Makefile.bsd,v retrieving revision 1.6 retrieving revision 1.7 diff -u -d -r1.6 -r1.7 --- Makefile.bsd 26 Dec 2003 10:07:58 -0000 1.6 +++ Makefile.bsd 26 Dec 2003 22:56:43 -0000 1.7 @@ -33,6 +33,12 @@ src/exceptions/SocketException.o \ src/server/network/Socket.o \ src/server/network/ServerSocket.o \ + src/server/http/HttpConstants.o \ + src/server/http/HttpRequest.o \ + src/server/http/HttpResponse.o \ + src/server/http/HttpSocket.o \ + src/server/http/HttpServerSocket.o \ + src/server/http/TextWriter.o \ src/test/TestHandler.o \ src/test/TestException.o \ src/test/TestThread.o \ Index: Makefile.osx =================================================================== RCS file: /cvsroot/sharedaemon/sharedaemon-ui-web/Makefile.osx,v retrieving revision 1.6 retrieving revision 1.7 diff -u -d -r1.6 -r1.7 --- Makefile.osx 26 Dec 2003 10:07:58 -0000 1.6 +++ Makefile.osx 26 Dec 2003 22:56:43 -0000 1.7 @@ -33,6 +33,12 @@ src/exceptions/SocketException.o \ src/server/network/Socket.o \ src/server/network/ServerSocket.o \ + src/server/http/HttpConstants.o \ + src/server/http/HttpRequest.o \ + src/server/http/HttpResponse.o \ + src/server/http/HttpSocket.o \ + src/server/http/HttpServerSocket.o \ + src/server/http/TextWriter.o \ src/test/TestHandler.o \ src/test/TestException.o \ src/test/TestThread.o \ Index: ChangeLog =================================================================== RCS file: /cvsroot/sharedaemon/sharedaemon-ui-web/ChangeLog,v retrieving revision 1.8 retrieving revision 1.9 diff -u -d -r1.8 -r1.9 --- ChangeLog 26 Dec 2003 10:31:16 -0000 1.8 +++ ChangeLog 26 Dec 2003 22:56:43 -0000 1.9 @@ -24,6 +24,10 @@ ----------------------------------------------------------- 26/12/2003 Mikael Barbeaux + * Added a test program for Http sockets. + * Fixed a problem about accepting sockets which were an infinite process. + * Implemented main http controls : HttpRequest, HttpResponse, HttpSocket, + HttpServerSocket and Writers * Fixed win32 sockets initialization bug. * Updated Makefiles * Implemented Socket, ServerSocket and SocketException classes. |
Update of /cvsroot/sharedaemon/sharedaemon-ui-web/src/exceptions In directory sc8-pr-cvs1:/tmp/cvs-serv3848/src/exceptions Modified Files: SocketException.h SocketException.cpp Added Files: WriterException.h HttpException.cpp HttpException.h WriterException.cpp Log Message: 26/12/2003 Mikael Barbeaux * Added a test program for Http sockets. * Fixed a problem about accepting sockets which were an infinite process. * Implemented main http controls : HttpRequest, HttpResponse, HttpSocket, HttpServerSocket and Writers --- NEW FILE: WriterException.h --- /* * This file is part of webInterface. * Copyright (C) 2003 Mikael Barbeaux <mik...@us...> * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ #ifndef _WRITER_EXCEPTION_H_ #define _WRITER_EXCEPTION_H_ #include "Exception.h" enum WriterError { NotClosedWriterExcp, ClosedWriterExcp }; /** * Defines a Exception for handling writer errors. */ class WriterException : public Exception { public: /** * Creates a Writer exception handler. * * @param eCode - code of this exception * @param message - Message to send to this exception * @param origin - Method who throws the exception */ WriterException(WriterError eCode, const string& message, const string& origin); /** * Returns the message sent by this Writer exception. * * @return const string */ virtual const string getMessage() const; }; #endif --- NEW FILE: HttpException.cpp --- /* * This file is part of webInterface. * Copyright (C) 2003 Mikael Barbeaux <mik...@us...> * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ #include "HttpException.h" #include <sstream> using namespace std; /** * Creates a HttpException object with the given error code and * message. */ HttpException::HttpException(HttpError eCode, const string& message, const string& origin) : Exception(eCode, message, origin) { } /** * Returns the message sent by this exception, depending the * error code sent. */ const string HttpException::getMessage() const { ostringstream message; // Build error message depending error code message << "-----------------------" << endl; message << "WARNING : HttpException thrown !!" << endl; message << "| Type : "; switch(code) { default: message << "UnknownError"; break; } message << endl << "| From : " << origin << endl; message << "| Message : " << what() << endl; message << "-----------------------" << endl; return message.str(); } --- NEW FILE: HttpException.h --- /* * This file is part of webInterface. * Copyright (C) 2003 Mikael Barbeaux <mik...@us...> * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ #ifndef _HTTP_EXCEPTION_H_ #define _HTTP_EXCEPTION_H_ #include "Exception.h" enum HttpError { NotHttpRequestExcp }; /** * Defines a Exception for handling http errors. */ class HttpException : public Exception { public: /** * Creates a Http exception handler. * * @param eCode - code of this exception * @param message - Message to send to this exception * @param origin - Method who throws the exception */ HttpException(HttpError eCode, const string& message, const string& origin); /** * Returns the message sent by this Http exception. * * @return const string */ virtual const string getMessage() const; }; #endif --- NEW FILE: WriterException.cpp --- /* * This file is part of webInterface. * Copyright (C) 2003 Mikael Barbeaux <mik...@us...> * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ #include "WriterException.h" #include <sstream> using namespace std; /** * Creates a WriterException object with the given error code and * message. */ WriterException::WriterException(WriterError eCode, const string& message, const string& origin) : Exception(eCode, message, origin) { } /** * Returns the message sent by this exception, depending the * error code sent. */ const string WriterException::getMessage() const { ostringstream message; // Build error message depending error code message << "-----------------------" << endl; message << "WARNING : WriterException thrown !!" << endl; message << "| Type : "; switch(code) { case NotClosedWriterExcp: message << "NotClosedWriter error"; break; case ClosedWriterExcp: message << "ClosedWriter error"; break; default: message << "UnknownError"; break; } message << endl << "| From : " << origin << endl; message << "| Message : " << what() << endl; message << "-----------------------" << endl; return message.str(); } Index: SocketException.h =================================================================== RCS file: /cvsroot/sharedaemon/sharedaemon-ui-web/src/exceptions/SocketException.h,v retrieving revision 1.1 retrieving revision 1.2 diff -u -d -r1.1 -r1.2 --- SocketException.h 26 Dec 2003 10:04:50 -0000 1.1 +++ SocketException.h 26 Dec 2003 22:56:42 -0000 1.2 @@ -33,7 +33,8 @@ CantBindSockExcp, UnboundSockExcp, CantListenSockExcp, - NotListeningSockExcp + NotListeningSockExcp, + NotAcceptedSockExcp }; /** Index: SocketException.cpp =================================================================== RCS file: /cvsroot/sharedaemon/sharedaemon-ui-web/src/exceptions/SocketException.cpp,v retrieving revision 1.1 retrieving revision 1.2 diff -u -d -r1.1 -r1.2 --- SocketException.cpp 26 Dec 2003 10:04:50 -0000 1.1 +++ SocketException.cpp 26 Dec 2003 22:56:42 -0000 1.2 @@ -40,6 +40,9 @@ message << "WARNING : SocketException thrown !!" << endl; message << "| Type : "; switch(code) { + case NotAcceptedSockExcp: + message << "NotAcceptedSocket error"; + break; case InvalidSockExcp: message << "InvalidSocket error"; break; |
From: <mik...@us...> - 2003-12-26 22:56:46
|
Update of /cvsroot/sharedaemon/sharedaemon-ui-web/src/test In directory sc8-pr-cvs1:/tmp/cvs-serv3848/src/test Modified Files: TestSocket.cpp Added Files: TestHttpSocket.h TestHttpSocket.cpp Log Message: 26/12/2003 Mikael Barbeaux * Added a test program for Http sockets. * Fixed a problem about accepting sockets which were an infinite process. * Implemented main http controls : HttpRequest, HttpResponse, HttpSocket, HttpServerSocket and Writers --- NEW FILE: TestHttpSocket.h --- /* * This file is part of webInterface. * Copyright (C) 2003 Mikael Barbeaux <mik...@us...> * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ #ifndef _TEST_HTTP_SOCKET_H_ #define _TEST_HTTP_SOCKET_H_ #include "TestHandler.h" /** * Defines a test class for http sockets. */ class TestHttpSocket : public TestHandler { public: /** * Creates a test for http sockets. */ TestHttpSocket(); /** * Destructor for TestHttpSocket */ ~TestHttpSocket(); /** * Runs the tester. */ virtual void test(); }; #endif --- NEW FILE: TestHttpSocket.cpp --- /* * This file is part of webInterface. * Copyright (C) 2003 Mikael Barbeaux <mik...@us...> * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ #include "TestHttpSocket.h" #include "../exceptions/Exception.h" #include "../server/http/HttpServerSocket.h" #include "../server/http/HttpSocket.h" #include <iostream> using namespace std; #include <stdio.h> /** * Constructor for TestHttpSocket. */ TestHttpSocket::TestHttpSocket() : TestHandler() { } /** * Destructor for TestHttpSocket. */ TestHttpSocket::~TestHttpSocket() { } /** * Runs the tester */ void TestHttpSocket::test() { /** This test tries to launch a thread * and goes on working. */ cout << endl; cout << "ShareDaemon Web Interface - Test" << endl << endl; cout << "Test for HTTP sockets... " << endl; cout << "The tester will create a socket, listening for connections on " << endl; cout << "port 4589, that will handler http connections. Open your " << endl; cout << "favourite browser and wait for the socket to start. Then, try" << endl; cout << "to open \"http://localhost:4589\" with your browser to test" << endl; cout << "the socket." << endl; cout << "Press enter to continue" << endl; getchar(); cout << endl << endl; try { cout << "Creates a http server on port 4589..." << endl; HttpServerSocket server(4589); cout << "Starts the server for 30 seconds..." << endl; server.start(); for(int i=0;i<20;i++) { cout << "Waited " << i << " seconds..." <<endl; Thread::sleep(1); } cout << "Closing server..." << endl; server.stop(); } catch(Exception& e) { // display the exception message cout << e.getMessage() << endl; } cout << "Could you connect to the socket and did you see the response ?" << endl; cout << " If no => VERY BAD :(" << endl; cout << " If yes => Test worked !! :D" << endl << endl; cout << "Press enter to continue" << endl; getchar(); } Index: TestSocket.cpp =================================================================== RCS file: /cvsroot/sharedaemon/sharedaemon-ui-web/src/test/TestSocket.cpp,v retrieving revision 1.1 retrieving revision 1.2 diff -u -d -r1.1 -r1.2 --- TestSocket.cpp 26 Dec 2003 10:04:51 -0000 1.1 +++ TestSocket.cpp 26 Dec 2003 22:56:42 -0000 1.2 @@ -78,7 +78,7 @@ } catch(Exception& e) { // display the exception message - cout << e.getMessage() << endl; + //cout << e.getMessage() << endl; } cout << "Could you connect to the socket and did you see the response ?" << endl; cout << " If no => VERY BAD :(" << endl; |
From: <mik...@us...> - 2003-12-26 22:56:46
|
Update of /cvsroot/sharedaemon/sharedaemon-ui-web/src In directory sc8-pr-cvs1:/tmp/cvs-serv3848/src Modified Files: Main.cpp Log Message: 26/12/2003 Mikael Barbeaux * Added a test program for Http sockets. * Fixed a problem about accepting sockets which were an infinite process. * Implemented main http controls : HttpRequest, HttpResponse, HttpSocket, HttpServerSocket and Writers Index: Main.cpp =================================================================== RCS file: /cvsroot/sharedaemon/sharedaemon-ui-web/src/Main.cpp,v retrieving revision 1.6 retrieving revision 1.7 diff -u -d -r1.6 -r1.7 --- Main.cpp 26 Dec 2003 10:33:47 -0000 1.6 +++ Main.cpp 26 Dec 2003 22:56:43 -0000 1.7 @@ -21,6 +21,7 @@ #include "test/TestThread.h" #include "test/TestSemaphore.h" #include "test/TestSocket.h" +#include "test/TestHttpSocket.h" #include <iostream> #include <string> using namespace std; @@ -33,7 +34,8 @@ cout << " 1. Exceptions test" << endl; cout << " 2. Threads test" << endl; cout << " 3. Semaphores test" << endl; - cout << " 4. Sockets test" << endl; + cout << " 4. Generic Sockets test" << endl; + cout << " 5. HTTP sockets test" << endl; cout << "Enter your choice : "; bool cont = true; @@ -72,6 +74,21 @@ #endif TestSocket test_sock; test_sock.test(); + cont = false; + } + else if(command == "5") { + #ifdef _WIN32_ + // Init WINSOCK + WORD winsockminversion = MAKEWORD(2,0); + WSADATA wsaData; + int WSockStatus = WSAStartup(winsockminversion, &wsaData); + if(WSockStatus != 0) { + cout << "ERROR_WINSOCK" << std::endl; + return -1; + } + #endif + TestHttpSocket test_http; + test_http.test(); cont = false; } else cout << "Invalid choice... Enter your choice : "; |
From: <mik...@us...> - 2003-12-26 22:56:46
|
Update of /cvsroot/sharedaemon/sharedaemon-ui-web/src/server/http In directory sc8-pr-cvs1:/tmp/cvs-serv3848/src/server/http Added Files: HttpConstants.h HttpResponse.h TextWriter.cpp Writer.h HttpConstants.cpp TextWriter.h HttpServerSocket.cpp HttpResponse.cpp HttpRequest.cpp HttpSocket.h HttpSocket.cpp HttpRequest.h HttpServerSocket.h Log Message: 26/12/2003 Mikael Barbeaux * Added a test program for Http sockets. * Fixed a problem about accepting sockets which were an infinite process. * Implemented main http controls : HttpRequest, HttpResponse, HttpSocket, HttpServerSocket and Writers --- NEW FILE: HttpConstants.h --- /* * This file is part of webInterface. * Copyright (C) 2003 Mikael Barbeaux <mik...@us...> * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ #ifndef _HTTP_CONSTANTS_H_ #define _HTTP_CONSTANTS_H_ #include <string> #include <sstream> #include <map> using namespace std; /** * Http Methods */ enum HttpMethod { GET, POST, HEAD, UNKNOWN }; /** * Http response codes */ enum HttpResponseCode { ACCEPTED = 200, // request accepted BAD_GATEWAY = 502, // bad gateway BAD_METHOD = 405, // method not allowed BAD_REQUEST = 400, // bad request CLIENT_TIMEOUT = 408, // request timeout CONFLIT = 409, // conflit CREATED = 201, // created ENTITY_TOO_LARGE = 413, // request entity too large FORBIDDEN = 403, // forbidden GATEWAY_TIMEOUT = 504, // gateway timeout GONE = 410, // gone INTERNAL_ERROR = 500, // internal error LENGTH_REQUIRED = 411, // length required MOVED_PERM = 301, // moved permanently MOVED_TEMP = 302, // moved temporary MULT_CHOICE = 300, // Multiple choice NO_CONTENT = 204, // no content NO_ACCEPTABLE = 406, // not acceptable NOT_AUTHORITATIVE = 203, // no authoritative information NOT_FOUND = 404, // not found NOT_IMPLEMENTED = 501, // not implemented NOT_MODIFIED = 304, // not modified OK = 200, // ok PARTIAL = 206, // partial content PAYMENT_REQUIRED = 402, // payment required PRECON_FAILED = 412, // precondiion failed PROXY_AUTH = 407, // proxy authentification required REQ_TOO_LARGE = 414, // request uri too large RESET = 405, // reset content SEE_OTHER = 303, // see other UNAUTHORIZED = 401, // unauthorized UNAVAILABLE = 503, // service unavailable UNSUPPORTED_TYPE = 415, // unsupported media type USE_PROXY = 305, // use proxy VERSION = 505 // http version not supported }; /** * Rules for parsing */ #define SP (char) 32 // space #define CR (char) 13 // carriage return #define LF (char) 10 // linefeed #define CRLF "\n" #define HT (char) 9 // horizontal tab /** * Defines a unique object for handling HttpConstants. * Implements the Singleton pattern. */ class HttpConstants { public: /** * Returns the unique instance of HttpConstants. * * @return HttpConstants* */ static HttpConstants* getInstance(); /** * Returns the description of a http status code. * * @return string */ string getDescription(HttpResponseCode code); /** * Returns the string representation of this code. * * @return string */ string codeToString(HttpResponseCode code); protected: /** * Creates a HttpConstants object. * This constructor is protected in order * to be sure only one instance will be created. */ HttpConstants(); /** * Copy constructor. * Also protected for same reasons as above. */ HttpConstants(const HttpConstants&); /** * Affectation of HttpConstants. * Also protected for same reasons as above. */ HttpConstants& operator= (const HttpConstants&); private: /** * The unique instance of HttpConstants. * To retrieve this instance, use the * getInstance() method. */ static HttpConstants* instance; map<int, string> descriptions; }; #endif --- NEW FILE: HttpResponse.h --- /* * This file is part of webInterface. * Copyright (C) 2003 Mikael Barbeaux <mik...@us...> * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ #ifndef _HTTP_RESPONSE_H_ #define _HTTP_RESPONSE_H_ #include "HttpConstants.h" #include "Writer.h" #include <string> #include <map> using namespace std; /** * Defines a abstract http response class * to be handle by the webserver. * A HTTP response is composed by: * * 1) A HTTP protocol version * 2) A status code, indicating response's type. * 3) A description of this status code. * 4) A list of headers, identifying the response. * 5) Eventually a body, containing datas resolved * by the request processing. * * This class is abstract because a response's body * can be full text or bytes array, thus it needs * differenciation. * To use a http response object that handles plain text, * such as text or html, use the HttpTextResponse object. * To use a http response object with bytes array as body, * use the HttpBytesResponse object. */ class HttpResponse { protected: // HTTP version used in this response string version; // HTTP status code of this response HttpResponseCode code; // description of this status code string code_description; // header parameters map<string, string> headers; // writer associated to this response Writer *writer; public: /** * Creates an empty HttpResponse object with * the given writer. * * @param writer_res */ HttpResponse(Writer *writer_res); /** * HttpResponse destructor. */ ~HttpResponse(); /** * Sets the HTTP version used in this response. * * @param string - HTTP version */ void setVersion(string http_version); /** * Sets the HTTP status code of this response. * * @param HttpResponseCode */ void setResponseCode(HttpResponseCode resp_code); /** * Adds the given header name and value to the * response. * * @param name * @param value */ void addHeader(string name, string value); /** * Adds the given header name associated to a * int value. * * @param name * @param value */ void addHeaderInt(string name, int value); /** * Tests if the given header name exists. * * @param name * @return bool */ bool containsHeader(string name); /** * Returns or sets the character-encoding value. */ string getCharacterEncoding(); void setCharacterEncoding(string charset); /** * Returns or sets the content-type value. */ string getContentType(); void setContentType(string type); /** * Sets the content-length value. */ void setContentLength(unsigned long int length); /** * Returns the writer associated to this response. * * @return Writer* */ Writer *getWriter() { return writer; } /** * Returns the response's header to be sent to the client. * * @return string */ string getResponseHeader(); }; #endif --- NEW FILE: TextWriter.cpp --- /* * This file is part of webInterface. * Copyright (C) 2003 Mikael Barbeaux <mik...@us...> * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ #include "TextWriter.h" /** * Default constructor for TextWriter */ TextWriter::TextWriter() : Writer() { } /** * TextWriter destructor */ TextWriter::~TextWriter() { } /** * Adds data to the buffer * Throws a WriterException if the writer has * been closed. */ TextWriter& TextWriter::operator<< (const char text[]) throw (WriterException) { if(closed) throw WriterException(ClosedWriterExcp, "Writer is closed.", "TextWriter::<< char[]"); buffer << text; return *this; } /** * Adds data to the buffer * Throws a WriterException if the writer has been * closed. */ TextWriter& TextWriter::operator<< (const string& text) throw (WriterException) { if(closed) throw WriterException(ClosedWriterExcp, "Writer is closed.", "TextWriter::<< string"); buffer << text; return *this; } /** * Adds int to the buffer * Throws a WriterException if the writer has been * closed. */ TextWriter& TextWriter::operator<< (int& a) throw (WriterException) { if(closed) throw WriterException(ClosedWriterExcp, "Writer is closed.", "TextWriter::<< int"); buffer << a; return *this; } --- NEW FILE: Writer.h --- /* * This file is part of webInterface. * Copyright (C) 2003 Mikael Barbeaux <mik...@us...> * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ #ifndef _WRITER_H_ #define _WRITER_H_ #include "../../exceptions/WriterException.h" /** * Defines a Writer for http response object. * If you want to handler text responses, such as * plain text, css or html, use the TextWriter object. * To handler bytes response, use the BytesWriter object. */ class Writer { protected: // closed state bool closed; public: /** * Default constructor * Set closed state */ Writer() { closed = false; } /** * Writer destructor * Does nothing */ virtual ~Writer() {} /** * Returns the data in this Writer. * Abstract method, need to be implemented * into subclasses. * * @return char* * @throw WriterException */ virtual char* getBytes() throw (WriterException) = 0; /** * Returns the size of the buffer * Must be implemented into subclasses * * @return unsigned long int * @throw WriterException */ virtual unsigned long int getSize() throw (WriterException) = 0; /** * Close the Writer */ void close() { closed = true; } /** * Tests if the Writer is closed. * * @return bool */ bool isClosed() { return closed; } }; #endif --- NEW FILE: HttpConstants.cpp --- /* * This file is part of webInterface. * Copyright (C) 2003 Mikael Barbeaux <mik...@us...> * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ #include "HttpConstants.h" // Init instance HttpConstants *HttpConstants::instance = 0; /** * Returns the unique instance of HttpConstants. * * @return HttpConstants* */ HttpConstants *HttpConstants::getInstance() { if(instance == 0) instance = new HttpConstants(); return instance; } /** * Creates a HttpConstants object. * This constructor is protected in order * to be sure only one instance will be created. */ HttpConstants::HttpConstants() { descriptions[100] = "Continue"; descriptions[101] = "Switching Protocols"; descriptions[200] = "OK"; descriptions[201] = "Created"; descriptions[202] = "Accepted"; descriptions[203] = "Non-Authoritative Information"; descriptions[204] = "No Content"; descriptions[205] = "Reset Content"; descriptions[206] = "Partial Content"; descriptions[300] = "Multiple Choices"; descriptions[301] = "Moved Permanently"; descriptions[302] = "Found"; descriptions[303] = "See Other"; descriptions[304] = "Not Modified"; descriptions[305] = "Use Proxy"; descriptions[307] = "Temporary Redirect"; descriptions[400] = "Bad Request"; descriptions[401] = "Unauthorized"; descriptions[402] = "Payment Required"; descriptions[403] = "Forbidden"; descriptions[404] = "Not Found"; descriptions[405] = "Method Not Allowed"; descriptions[406] = "Not Acceptable"; descriptions[407] = "Proxy Authentification Required"; descriptions[408] = "Request Time-Out"; descriptions[409] = "Conflict"; descriptions[410] = "Gone"; descriptions[411] = "Length Required"; descriptions[412] = "Precondition Failed"; descriptions[413] = "Request Entity Too Large"; descriptions[414] = "Request-URI Too Large"; descriptions[415] = "Unsupported Media Type"; descriptions[416] = "Requested ranfe not satisfiable"; descriptions[417] = "Expectation Failed"; descriptions[500] = "Internal Server Error"; descriptions[501] = "Not Implemented"; descriptions[502] = "Bad Gateway"; descriptions[503] = "Service unavailable"; descriptions[504] = "Gateway Time-out"; descriptions[505] = "HTTP Version not supported"; } /** * Returns the description of a http status code. */ string HttpConstants::getDescription(HttpResponseCode code) { map<int, string>::iterator it = descriptions.find(code); if(it != descriptions.end()) return (string) (*it).second; else return ""; } /** * Returns the string representation of this code. */ string HttpConstants::codeToString(HttpResponseCode code) { ostringstream s; s << code; return s.str(); } --- NEW FILE: TextWriter.h --- /* * This file is part of webInterface. * Copyright (C) 2003 Mikael Barbeaux <mik...@us...> * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ #ifndef _TEXT_WRITER_H #define _TEXT_WRITER_H #include "Writer.h" #include "../../exceptions/WriterException.h" #include <string> #include <sstream> using namespace std; /** * Defines a Writer for handling text buffer only. * To handler bytes buffer, use the BytesWriter * object instead. */ class TextWriter : public Writer { private: // buffer for text data. ostringstream buffer; public: /** * Default constructor for TextWriter */ TextWriter(); /** * TextWriter destructor */ ~TextWriter(); /** * Adds data to the buffer * * @param const char[] * @return TextWriter * @throw WriterException */ TextWriter& operator<< (const char text[]) throw (WriterException); /** * Adds data to the buffer * * @param const string& * @return TextWriter * @throw WriterException */ TextWriter& operator<< (const string& text) throw (WriterException); /** * Adds int to the buffer * * @param int& * @return TextWriter * @throw WriterException */ TextWriter& operator<< (int& a) throw (WriterException); /** * Returns the buffer's content. * * @return char* */ virtual char* getBytes() throw (WriterException) { if(! closed) throw WriterException(NotClosedWriterExcp, "Writer isn't closed.", "TextWriter::getBytes"); return (char*) buffer.str().c_str(); } /** * Returns the size of the buffer * Must be implemented into subclasses * * @return unsigned long int */ virtual unsigned long int getSize() throw (WriterException) { if(! closed) throw WriterException(NotClosedWriterExcp, "Writer isn't closed.", "TextWriter::getBytes"); return (unsigned long int) buffer.str().size(); } }; #endif --- NEW FILE: HttpServerSocket.cpp --- /* * This file is part of webInterface. * Copyright (C) 2003 Mikael Barbeaux <mik...@us...> * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ #include "HttpServerSocket.h" #include "TextWriter.h" #include <iostream> using namespace std; /** * Constructor * Init server port. */ HttpServerSocket::HttpServerSocket(int port) : ServerSocket(), Thread() { server_port = port; } /** * HttpServerSocket destructor * Waits for thread to terminate. */ HttpServerSocket::~HttpServerSocket() throw (ThreadException) { // wait for the thread to terminate wait(); } /** * Starts the server. * Implements the thread execution code. */ void HttpServerSocket::run() { // Starts the server validate(); bind(server_port); listen(20); while(true) { // sets a cancel point setCancelPoint(); // we accept a connection HttpSocket *client = 0; try { try { client = (HttpSocket*) accept(); } catch(SocketException& se) { } if(client == 0) continue; // process the connection //theManager->addConnection(client); cout << "Socket accepted !" << endl; HttpRequest request; cout << "Receiving HTTP request..." << endl; client->receiveHttpRequest(request); cout << "Reveiced mesage : " << endl; cout << request.toString() << endl; cout << "Send response" << endl; TextWriter *writer = new TextWriter; *writer << "<html><body>Request received !</body></html>"; writer->close(); HttpResponse response(writer); response.setContentType("text/html"); cout << "ok" << endl; client->sendHttpResponse(response); cout << "Closing socket..." << endl; client->close(); delete client; } catch(Exception& e) { cout << e.getMessage() << endl; } } } /** * Starts the server. */ void HttpServerSocket::start() { Thread::start(); } /** * Stops the server. */ void HttpServerSocket::stop() throw (SocketException) { close(); cancel(); } --- NEW FILE: HttpResponse.cpp --- /* * This file is part of webInterface. * Copyright (C) 2003 Mikael Barbeaux <mik...@us...> * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ #include "HttpResponse.h" /** * Default constructor for HttpResponse * Does nothing... */ HttpResponse::HttpResponse(Writer *writer_res) { writer = writer_res; // we suppose at first that the response is '200 OK' code = OK; code_description = HttpConstants::getInstance()->getDescription(OK); version = "HTTP/1.1"; } /** * HttpResponse destructor */ HttpResponse::~HttpResponse() { delete writer; } /** * Sets the HTTP version used in this version. */ void HttpResponse::setVersion(string http_version) { version = http_version; } /** * Sets the HTTP status code of this response. * It will also set automatically the description * of this status code. */ void HttpResponse::setResponseCode(HttpResponseCode resp_code) { code = resp_code; code_description = HttpConstants::getInstance()->getDescription(resp_code); } /** * Adds the given header name and value to the * response. */ void HttpResponse::addHeader(string name, string value) { headers[name] = value; } /** * Adds the given header name associated to a * int value. */ void HttpResponse::addHeaderInt(string name, int value) { string s = ""+value; addHeader(name, s); } /** * Tests if the given header name exists. */ bool HttpResponse::containsHeader(string name) { map<string, string>::iterator it = headers.find(name); return (it != headers.end()); } /** * Returns or sets the character-encoding value. */ string HttpResponse::getCharacterEncoding() { map<string, string>::iterator it = headers.find("Content-Encoding"); if(it != headers.end()) return (*it).second; else return ""; } void HttpResponse::setCharacterEncoding(string charset) { headers["Content-Encoding"] = charset; } /** * Returns or sets the content-type value. */ string HttpResponse::getContentType() { map<string, string>::iterator it = headers.find("Content-Type"); if(it != headers.end()) return (*it).second; else return ""; } void HttpResponse::setContentType(string type) { headers["Content-Type"] = type; } /** * Sets the content-length value. */ void HttpResponse::setContentLength(unsigned long int length) { string s = "" + length; headers["Content-Length"] = s; } /** * Returns the response's header to be sent to the client. */ string HttpResponse::getResponseHeader() { string header = (version + " "); header += HttpConstants::getInstance()->codeToString(code); header += (" " + code_description + "\n"); map<string, string>::iterator it = headers.begin(); while(it != headers.end()) { header += ((string) ((*it).first) + ": " + (string) ((*it).second) + "\n"); it++; } header += "\n"; return header; } --- NEW FILE: HttpRequest.cpp --- /* * This file is part of webInterface. * Copyright (C) 2003 Mikael Barbeaux <mik...@us...> * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ #include "HttpRequest.h" #include <iostream> #include <string> using namespace std; /** * Function that counts the number of * occurrences of a string into another * string. * * @param s - The string * @param tok - Token to count into the string * @return int - number of occurrences of tok into s */ int nbOccurs(string s, string tok) { int nb = 0; unsigned int start = 0, pos; while((pos = s.find(tok, start)) != string::npos) { nb++; start = pos+1; } return nb++; } /** * Creates an empty HttpRequest. * Does nothing... */ HttpRequest::HttpRequest() { } /** * HttpRequest destructor * Does nothing... */ HttpRequest::~HttpRequest() { } /** * Parses data to build the HTTP request. * Throws a HttpException if the data isn't * Http compliant. * It returns true if it still needs some bytes to * be received ( typically, request's body is sent * in another packet ). * * METHOD SP URI SP VERSION CRLF * *( name ":" value CRLF) * CRLF * [body] * */ bool HttpRequest::parseData(char* data) throw(HttpException) { string req_str = data; unsigned int start = 0; unsigned int pos; // Method pos = req_str.find(SP, start); if(pos == string::npos) throw HttpException(NotHttpRequestExcp, "The received request isn't HTTP compliant", "HttpRequest::parseData"); string meth = req_str.substr(start, pos - start); if(meth == "GET") method = GET; else if(meth == "POST") method = POST; else if(meth == "HEAD") method = HEAD; else method = UNKNOWN; // URI start = pos+1; pos = req_str.find(SP, start); if(pos == string::npos) throw HttpException(NotHttpRequestExcp, "The received request isn't HTTP compliant", "HttpRequest::parseData"); string r_uri = req_str.substr(start, pos - start); // Retrieving parameters if any... unsigned int ind; if((ind = r_uri.find("?", 0)) != string::npos) { // parse query string for parameters parseQueryString(r_uri.substr(ind+1,r_uri.length())); } uri = r_uri; // HTTP version start = pos+1; pos = req_str.find(CRLF, start); if(pos == string::npos) throw HttpException(NotHttpRequestExcp, "The received request isn't HTTP compliant", "HttpRequest::parseData"); version = req_str.substr(start, pos - start - 1); // Headers start = pos+1; while((pos = req_str.find(CRLF, start)) != (start+1)) { // There still have some headers, retrieving it... string line = req_str.substr(start, pos - start - 1); unsigned int temp; if((temp = line.find(":",0)) == string::npos) throw HttpException(NotHttpRequestExcp, "The received request isn't HTTP compliant", "HttpRequest::parseData"); // parse header values headers[line.substr(0,temp)] = line.substr(temp+2, line.length()); start = pos+1; } // Body start = pos+1; // Size of body unsigned long bytes = getHeaderInt("Content-Length"); if(bytes == 0) { // no body... body = ""; return false; } // if body still needs to be received... else if(start >= req_str.length()) { body = ""; return true; } // body is received... else { body = req_str.substr(start, req_str.length()); // still needs some bytes... if(body.length() < bytes) return true; // everything is received ! else return false; } } /** * Parses the body data to build request's body. * Typically, browsers send request's header and request's * body in two differents packets. This method retrieves * the body that didn't come into request's header. * Returns true if body is still not complete. */ bool HttpRequest::parseBody(char* data) { // Size of body unsigned long bytes = getHeaderInt("Content-Length"); body += data; if(body.length() < bytes) return true; else { // Delete last CRLF if exists.. if(body.find("\n",body.length()-1)) body = body.substr(0,body.length()-1); // parse query string parseQueryString(body); return false; } } /** * Converts the request into a string. * For debugging purposes only. */ string HttpRequest::toString() { // building output string string out = ""; switch(method) { case GET: out += "GET "; break; case HEAD: out += "HEAD "; break; case POST: out += "POST "; break; default: out += "UNKNOWN "; break; } out += (uri + " "); out += (version + "\n"); map<string, string>::iterator it = headers.begin(); while(it != headers.end()) { out += (((string) (*it).first) + ": " + ((string) (*it).second) + "\n"); it++; } out += "\n"; out += body; return out; } /** * Parses uri for finding query parameters. * Throws a HttpException if query string isn't * http compliant. * * name "=" value *( "&" name "=" value ) */ void HttpRequest::parseQueryString(string query) throw (HttpException) { // parse string unsigned int start = 0; unsigned int pos; while((pos = query.find("&",start)) != string::npos) { unsigned int pos1; if(((pos1 = query.find("=",start)) == string::npos) || (pos1 >= pos)) throw HttpException(NotHttpRequestExcp, "The received request isn't HTTP compliant", "HttpRequest::parseData"); parameters[query.substr(start,pos1-start)] = query.substr(pos1+1,pos-pos1-1); start = pos+1; } unsigned int pos1; if(((pos1 = query.find("=",start)) == string::npos) || (pos1 >= query.length())) throw HttpException(NotHttpRequestExcp, "The received request isn't HTTP compliant", "HttpRequest::parseData"); parameters[query.substr(start,pos1-start)] = query.substr(pos1+1,query.length()); } /** * Returns value of this header name * If there are multiple values for this header * name, it will return the first value of it. * If there isn't such a header name, it will * return an empty string. */ string HttpRequest::getHeader(string name) { map<string, string>::iterator it = headers.find(name); if(it == headers.end()) return ""; string values = (string) (*it).second; unsigned int pos; if((pos = values.find(",", 0)) != string::npos) return values.substr(0, pos); else return values; } /** * Returns value of this header name as a int. * If there are multiple values for this header * name, it will return the first int value of it. * If there isn't such a header name, or if convertion * to int isn't possible, it will return 0. */ long HttpRequest::getHeaderInt(string name) { string v = getHeader(name); if(v == "") return 0; else return atol(v.c_str()); } /** * Returns all headers names. */ vector<string> HttpRequest::getHeaderNames() { map<string, string>::iterator it = headers.begin(); vector<string> names(headers.size()); int i=0; while(it != headers.end()) { names[i] = (*it).first; i++; it++; } return names; } /** * Returns all values of this header name. If no such * header name exists, it returns an empty vector. */ vector<string> HttpRequest::getHeaders(string name) { map<string, string>::iterator it = headers.find(name); if(it == headers.end()) { // returns a empty vector vector<string> values(0); return values; } string values_str = (string) (*it).second; unsigned int start = 0; unsigned int pos; int i=0; vector<string> values(nbOccurs(values_str, ",")+1); while((pos = values_str.find(",", start)) != string::npos) { values[i] = values_str.substr(start, pos-start); start = pos+1; i++; } values[i] = values_str.substr(start, values_str.length()); return values; } /** * Returns value of the given parameter name. * If this parameter doesn't exist, it will * return an empty string. */ string HttpRequest::getParameter(string name) { map<string, string>::iterator it = parameters.find(name); if(it == parameters.end()) return ""; else return (string) (*it).second; } /** * Returns all the parameters into a map. */ map<string, string> HttpRequest::getParameterMap() { return parameters; } /** * Returns all parameters' names * If there isn't any parameters, it will return * an empty vector. */ vector<string> HttpRequest::getParameterNames() { map<string, string>::iterator it = parameters.begin(); vector<string> names(parameters.size()); int i=0; while(it != parameters.end()) { names[i] = (*it).first; i++; it++; } return names; } /** * Returns the uri without any parameters if * exist. */ string HttpRequest::getURI() { unsigned int pos; if((pos = uri.find("?", 0)) != string::npos) return uri.substr(0, pos); else return uri; } --- NEW FILE: HttpSocket.h --- /* * This file is part of webInterface. * Copyright (C) 2003 Mikael Barbeaux <mik...@us...> * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ #ifndef _HTTP_SOCKET_H_ #define _HTTP_SOCKET_H_ #include "../network/Socket.h" #include "../../exceptions/SocketException.h" #include "../../exceptions/WriterException.h" #include "HttpRequest.h" #include "HttpResponse.h" /** * Defines a socket for the Http protocol. * It extends the generic socket implementation. * Now, it handles HttpRequest and HttpResponse * for communicating with clients, instead * of simple bytes arrays. */ class HttpSocket : protected Socket { public: /** * Create a unconnected Http socket. */ HttpSocket(); /** * Creates a http socket with the given * socket's id and address. */ HttpSocket(int id, sockaddr_in& address); /** * Http socket destructor */ ~HttpSocket(); /** * The following methods from Socket stays public : * isClosed * isValid * getId * close */ Socket::isClosed; Socket::isValid; Socket::getId; Socket::close; /** * Build the HttpRequest of this socket. * * @param HttpRequest */ void receiveHttpRequest(HttpRequest& request) throw (SocketException); /** * Send the HttpResponse of this socket. * * @param HttpResponse */ void sendHttpResponse(HttpResponse& response) throw (SocketException, WriterException); }; #endif --- NEW FILE: HttpSocket.cpp --- /* * This file is part of webInterface. * Copyright (C) 2003 Mikael Barbeaux <mik...@us...> * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ #include "HttpSocket.h" /** * Default constructor for Http sockets. * Does nothing for an unconnected socket. */ HttpSocket::HttpSocket() : Socket() { } /** * Creates a Http socket using the given socket's id * and the given address. */ HttpSocket::HttpSocket(int id, sockaddr_in& address) : Socket(id, address) { } /** * Default destructor for Http socket. * Does nothing. */ HttpSocket::~HttpSocket() { } /** * Process the request associated to this http socket. * It receives all data from the client and then * parse the data to build the associated http request. */ void HttpSocket::receiveHttpRequest(HttpRequest& request) throw (SocketException) { // Get the data from the client char *data = new char; receive(&data); // create the request object bool not_complete = request.parseData(data); delete[] data; // while body still not completely received... while(not_complete) { char *body = new char; // receive bytes again receive(&body); not_complete = request.parseBody(data); delete[] body; } } /** * Send the http response to the client socket. * It converts the response to bytes arrays and * send it to the client. */ void HttpSocket::sendHttpResponse(HttpResponse& response) throw (SocketException, WriterException) { // Get header string h = response.getResponseHeader(); char *header = (char*)h.c_str(); unsigned long int header_size = h.size(); // Get body Writer *writer = response.getWriter(); char *body = writer->getBytes(); unsigned long int body_size = writer->getSize(); // Send response to the client send(header, header_size); send(body, body_size); } --- NEW FILE: HttpRequest.h --- /* * This file is part of webInterface. * Copyright (C) 2003 Mikael Barbeaux <mik...@us...> * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ #ifndef _HTTP_REQUEST_H_ #define _HTTP_REQUEST_H_ #include "HttpConstants.h" #include "../../exceptions/HttpException.h" #include <string> #include <map> #include <vector> using namespace std; /** * Defines a http request to be handle by the webserver. * A HTTP request is composed by: * * 1) A method, typically GET, POST or HEAD. * 2) A URI to indicate the request target. * 3) A HTTP protocol version, typically HTTP/1.0 * 4) A list of headers, identifying the client. * 5) Eventually a body, containing datas from the POST * request. * */ class HttpRequest { private: // method of this http request HttpMethod method; // URI of the target. string uri; // request parameters map<string, string> parameters; // HTTP version used string version; // header parameters map<string, string> headers; // body string body; /** * Parses the query string to find parameters. * * @param query - the query string * @throw HttpException */ void parseQueryString(string query) throw (HttpException); public: /** * Creates a empty HttpRequest object. */ HttpRequest(); /** * HttpRequest destructor. */ ~HttpRequest(); /** * Parses data to build the http request. * * @param data - Data to parse * @return bool - if not yet complete * @throw HttpException */ bool parseData(char* data) throw (HttpException); /** * Parses body data to build the http request. * * @param data - Body data to parse * @return bool - if body not yet complete */ bool parseBody(char* data); /** * Converts this request into a string. * * @return string */ string toString(); /** * Returns value of this header name * * @param name - Name of this header * @return string - (first) value or empty string */ string getHeader(string name); /** * Returns value of this header name as a long * * @param name - Name of this header * @return long - (first) value or 0 */ long getHeaderInt(string name); /** * Returns all headers names. * * @return vector<string> - All headers names */ vector<string> getHeaderNames(); /** * Returns all values of this header name. * * @return vector<string> - All values of this * header name */ vector<string> getHeaders(string name); /** * Returns the method of this request. * * @return HttpMethod */ HttpMethod getMethod() { return method; } /** * Returns the complete uri of this request. * * @return string */ string getMethodURI() { return uri; } /** * Returns the uri without any parameters if * exist. * * @return string */ string getURI(); /** * Returns HTTP version used in this request. * * @return string */ string getHttpVersion() { return version; } /** * Returns value of the given parameter name. * * @param name - parameter's name * @return string - parameter's value */ string getParameter(string name); /** * Returns all the parameters into a map * * @return map<string, string> */ map<string, string> getParameterMap(); /** * Returns all parameters' names * * @return vector<string> */ vector<string> getParameterNames(); }; #endif --- NEW FILE: HttpServerSocket.h --- /* * This file is part of webInterface. * Copyright (C) 2003 Mikael Barbeaux <mik...@us...> * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ #ifndef _HTTP_SERVER_SOCKET_H_ #define _HTTP_SERVER_SOCKET_H_ #include "../network/ServerSocket.h" #include "HttpSocket.h" #include "../../exceptions/SocketException.h" #include "../../thread/Thread.h" #include "../../exceptions/ThreadException.h" /** * Defines a HttpServerSocket object. * A http server socket is a threaded ServerSocket * that accepts HttpSocket. */ class HttpServerSocket : protected ServerSocket, protected Thread { private: // server's port int server_port; protected: /** * Runs the server. * Implements the thread execution code. */ virtual void run(); public: /** * Creates a HttpServerSocket object on the * given port ( default is 80). */ HttpServerSocket(int port = 80); /** * HttpServerSocket destructor. */ ~HttpServerSocket() throw (ThreadException); /** * Starts the server. */ void start(); /** * Stops the server. */ void stop() throw (SocketException); }; #endif |