PageRenderTime 37ms CodeModel.GetById 14ms RepoModel.GetById 1ms app.codeStats 0ms

/mordor/examples/simpleappserver.cpp

http://github.com/mozy/mordor
C++ | 66 lines | 55 code | 10 blank | 1 comment | 12 complexity | c5de0c9ae9b90bc40aff2ea2d34be8c3 MD5 | raw file
Possible License(s): BSD-3-Clause
  1. // Copyright (c) 2010 - Mozy, Inc.
  2. #include "mordor/predef.h"
  3. #include <iostream>
  4. #include "mordor/config.h"
  5. #include "mordor/http/server.h"
  6. #include "mordor/iomanager.h"
  7. #include "mordor/main.h"
  8. #include "mordor/socket.h"
  9. #include "mordor/streams/memory.h"
  10. #include "mordor/streams/socket.h"
  11. #include "mordor/streams/transfer.h"
  12. using namespace Mordor;
  13. static std::map<URI, MemoryStream::ptr> g_state;
  14. static void httpRequest(HTTP::ServerRequest::ptr request)
  15. {
  16. const std::string &method = request->request().requestLine.method;
  17. const URI &uri = request->request().requestLine.uri;
  18. if (method == HTTP::GET || method == HTTP::HEAD) {
  19. std::map<URI, MemoryStream::ptr>::iterator it =
  20. g_state.find(uri);
  21. if (it == g_state.end()) {
  22. HTTP::respondError(request, HTTP::NOT_FOUND);
  23. } else {
  24. MemoryStream::ptr copy(new MemoryStream(it->second->buffer()));
  25. HTTP::respondStream(request, copy);
  26. }
  27. } else if (method == HTTP::PUT) {
  28. MemoryStream::ptr stream(new MemoryStream());
  29. transferStream(request->requestStream(), stream);
  30. g_state[uri] = stream;
  31. HTTP::respondError(request, HTTP::OK);
  32. } else {
  33. HTTP::respondError(request, HTTP::METHOD_NOT_ALLOWED);
  34. }
  35. }
  36. MORDOR_MAIN(int argc, char *argv[])
  37. {
  38. try {
  39. Config::loadFromEnvironment();
  40. IOManager ioManager;
  41. Socket s(ioManager, AF_INET, SOCK_STREAM);
  42. IPv4Address address(INADDR_ANY, 80);
  43. s.bind(address);
  44. s.listen();
  45. while (true) {
  46. Socket::ptr socket = s.accept();
  47. Stream::ptr stream(new SocketStream(socket));
  48. HTTP::ServerConnection::ptr conn(new HTTP::ServerConnection(stream,
  49. &httpRequest));
  50. conn->processRequests();
  51. }
  52. } catch (...) {
  53. std::cerr << boost::current_exception_diagnostic_information() << std::endl;
  54. }
  55. return 0;
  56. }