Ejemplo n.º 1
0
LRESULT CALLBACK mHpWebClient::WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam) {
  switch (uMsg) {
    case WM_SIZE: {
      mHpWebClient* win = reinterpret_cast<mHpWebClient*> (::GetWindowLongPtr(hwnd, GWLP_USERDATA));
      if (win) {
        win->Resize(LOWORD(lParam), HIWORD(lParam));
      }
      return 0;
    }

    case WM_CREATE: {
      return 0;
    }

    case WM_KEYDOWN: {
      TraceFunc("WM_KEYDOWN in mHpWebClient");
      Trace2("wParam: ", wParam);
      if (wParam == VK_TAB) {
        /*
           The following code is necessary to enable 'tabulator navigating' in forms.
           See also http://www.microsoft.com/0499/faq/faq0499.asp
           and the SendMessage part in the MessageLoop
        */
        IOleInPlaceActiveObject* ipao;
        IWebBrowser2  *webBrowser2;
        mHpWebClient* win = reinterpret_cast<mHpWebClient*> (::GetWindowLongPtr(hwnd, GWLP_USERDATA));
        if (win) {
          if (!win->browserObject_->QueryInterface(IID_IWebBrowser2, (void**)&webBrowser2)) {
            webBrowser2->QueryInterface(IID_IOleInPlaceActiveObject, reinterpret_cast<void**>(&ipao));
            if (ipao) {
              MSG m;
              m.message=WM_KEYDOWN;
              m.wParam = wParam;
              m.lParam = lParam;
              m.hwnd   = hwnd;

              ipao->TranslateAccelerator(&m);
            }
            else {
              ::MessageBox(0, "Failed to retrieve IOleInPlaceActiveObject in WM_KEYDOWN", 0, 0);
            }
          }
          return 0;
        }
        else {
          ::MessageBox(0, "Failed to retrieve webBrowser2 in WM_KEYDOWN", 0, 0);
        }
        return -1;
      }
      break;
    }
    
    case WM_APP: {
     
      TraceFunc("WM_APP called"); 
      mHpWebClient*  win               = reinterpret_cast<mHpWebClient* >(::GetWindowLongPtr(hwnd, GWLP_USERDATA));
      std::string* path_with_params  = reinterpret_cast<std::string*>(wParam);
      std::string path;
      std::map<std::string,std::string> params;

      Trace(std::string("path_with_params: ") + *path_with_params);

      SplitGetReq(*path_with_params, path, params);

      Trace(std::string("path: ") + path);

      std::string out_html;

      win->AppLink(path, out_html, params);

      win->HTML(out_html);

      // url is allocated in DOCHostHandler.cpp
      Trace("going to delete url");
      //delete url;
      delete path_with_params;
      // param_map is allocated in DOCHostHandler.cpp
      Trace("going to delete param_map");

      //delete param_map;
      Trace("param_map deleted");
      return 0;
    }
  }

  return(DefWindowProc(hwnd, uMsg, wParam, lParam));
}
Ejemplo n.º 2
0
unsigned webserver::Request(void* ptr_s) {
  Socket s = *(reinterpret_cast<Socket*>(ptr_s));
  
  std::string line = s.ReceiveLine();
  if (line.empty()) {
    return 1;
  }

  http_request req;

  if (line.find("GET") == 0) {
    req.method_="GET";
  }
  else if (line.find("POST") == 0) {
    req.method_="POST";
  }

  std::string path;
  std::map<std::string, std::string> params;

  size_t posStartPath = line.find_first_not_of(" ",3);

  SplitGetReq(line.substr(posStartPath), path, params);

  req.status_ = "202 OK";
  req.s_      = &s;
  req.path_   = path;
  req.params_ = params;

  static const std::string authorization   = "Authorization: Basic ";
  static const std::string accept          = "Accept: "             ;
  static const std::string accept_language = "Accept-Language: "    ;
  static const std::string accept_encoding = "Accept-Encoding: "    ;
  static const std::string user_agent      = "User-Agent: "         ;

  while(1) {
    line=s.ReceiveLine();

	cout << "Web service: receiving " << line << endl;

    if (line.empty()) break;

    unsigned int pos_cr_lf = line.find_first_of("\x0a\x0d");
    if (pos_cr_lf == 0) break;

    line = line.substr(0,pos_cr_lf);

    if (line.substr(0, authorization.size()) == authorization) {
      req.authentication_given_ = true;
      std::string encoded = line.substr(authorization.size());
      std::string decoded = base64_decode(encoded);

      unsigned int pos_colon = decoded.find(":");

      req.username_ = decoded.substr(0, pos_colon);
      req.password_ = decoded.substr(pos_colon+1 );
    }
    else if (line.substr(0, accept.size()) == accept) {
      req.accept_ = line.substr(accept.size());
    }
    else if (line.substr(0, accept_language.size()) == accept_language) {
      req.accept_language_ = line.substr(accept_language.size());
    }
    else if (line.substr(0, accept_encoding.size()) == accept_encoding) {
      req.accept_encoding_ = line.substr(accept_encoding.size());
    }
    else if (line.substr(0, user_agent.size()) == user_agent) {
      req.user_agent_ = line.substr(user_agent.size());
    }
  }

  request_func_(&req);

  std::stringstream str_str;
  str_str << req.answer_.size();

  time_t ltime;
  time(&ltime);
  tm* gmt= gmtime(&ltime);

  static std::string const serverName = "RenesWebserver (Windows)";

  char* asctime_remove_nl = asctime(gmt);
  asctime_remove_nl[24] = 0;

  s.SendBytes("HTTP/1.1 ");

  if (! req.auth_realm_.empty() ) {
    s.SendLine("401 Unauthorized");
    s.SendBytes("WWW-Authenticate: Basic Realm=\"");
    s.SendBytes(req.auth_realm_);
    s.SendLine("\"");
  }
  else {
    s.SendLine(req.status_);
  }
  s.SendLine(std::string("Date: ") + asctime_remove_nl + " GMT");
  s.SendLine(std::string("Server: ") +serverName);
  s.SendLine("Connection: close");
  s.SendLine("Content-Type: text/html; charset=ISO-8859-1");
  s.SendLine("Content-Length: " + str_str.str());
  s.SendLine("");
  s.SendLine(req.answer_);

  s.Close();
  

  return 0;
}
Ejemplo n.º 3
0
void* webserver::Request(void* ptr_s)
#endif
{
	Socket* s = (reinterpret_cast<Socket*>(ptr_s));

	std::string line = s->ReceiveLine();
	if (line.empty()) 
	{
		s->Close();
		delete s;
		return 0;
	}

	http_request req;
	std::string path;
	std::map<std::string, std::string> params;
	size_t posStartPath = 0;

	if (line.find("GET") == 0) 
	{
		req.method_="GET";
		posStartPath = line.find_first_not_of(" ",3);
	}
	else if (line.find("POST") == 0) 
	{
		req.method_="POST";
		posStartPath = line.find_first_not_of(" ",4);
	}

	SplitGetReq(line.substr(posStartPath), path, params);

	req.status_ = "202 OK";
	req.s_      = s;
	req.path_   = path;
	req.params_ = params;

	static const char authorization[]   = "Authorization: Basic ";
	static const char accept[]          = "Accept: ";
	static const char accept_language[] = "Accept-Language: ";
	static const char accept_encoding[] = "Accept-Encoding: ";
	static const char user_agent[]      = "User-Agent: ";
	static const char content_length[]  = "Content-Length: ";
	static const char content_type[]    = "Content-Type: ";

	while(1)
	{
		line=s->ReceiveLine();
		if (line.empty())
		{
			break;
		}

		unsigned int pos_cr_lf = line.find_first_of("\x0a\x0d");
		if (pos_cr_lf == 0) break;

		line = line.substr(0,pos_cr_lf);

		if (line.compare(0, sizeof(authorization) - 1, authorization) == 0) 
		{
			req.authentication_given_ = true;
			std::string encoded = line.substr(sizeof(authorization) - 1);
			std::string decoded = base64_decode(encoded);

			unsigned int pos_colon = decoded.find(":");

			req.username_ = decoded.substr(0, pos_colon);
			req.password_ = decoded.substr(pos_colon + 1);
		}
		else if (line.compare(0, sizeof(accept) - 1, accept) == 0)
		{
			req.accept_ = line.substr(sizeof(accept) - 1);
		}
		else if (line.compare(0, sizeof(accept_language) - 1, accept_language) == 0)
		{
			req.accept_language_ = line.substr(sizeof(accept_language) - 1);
		}
		else if (line.compare(0, sizeof(accept_encoding) - 1, accept_encoding) == 0)
		{
			req.accept_encoding_ = line.substr(sizeof(accept_encoding) - 1);
		}
		else if (line.compare(0, sizeof(user_agent) - 1, user_agent) == 0)
		{
			req.user_agent_ = line.substr(sizeof(user_agent) - 1);
		}
		else if (line.compare(0, sizeof(content_length) - 1, content_length) == 0)
		{
			req.content_length_ = atoi(line.substr(sizeof(content_length) - 1).c_str() );
		}
		else if (line.compare(0, sizeof(content_type) - 1, content_type) == 0)
		{
			req.content_type_ = line.substr(sizeof(content_type) - 1);
		}
	}

	if( (req.method_.compare("POST") == 0) && (req.content_length_ > 0) )
	{
		const char FormUrlEncoded[] = "application/x-www-form-urlencoded";
		// The only content type we can parse at the moment, the default HTML post data
		if( req.content_type_.substr(0, strlen(FormUrlEncoded)).compare( FormUrlEncoded ) == 0 )
		{
			std::string Content = s->ReceiveBytes( req.content_length_ );
			Content.insert( 0, "/ ?" ); // Little hack, inserts dummy URL so that SplitGetReq() can work with this content

			std::string dummy;
			std::map<std::string, std::string> post_params;
			SplitGetReq(Content, dummy, post_params);

			req.params_post_ = post_params;
		}
		else 
		{
			ParseMultipartFormData( req, s );
		}
	}

	request_func_(&req);

	std::stringstream str_str;
	str_str << req.answer_.size();

	time_t ltime;
	time(&ltime);
	tm* gmt= gmtime(&ltime);

#ifdef _WIN32
	static const char serverName[] = "MCServerWebAdmin (Windows)";
#elif __APPLE__
	static const char serverName[] = "MCServerWebAdmin (MacOSX)";
#else
	static const char serverName[] = "MCServerWebAdmin (Linux)";
#endif


	char* asctime_remove_nl = std::asctime(gmt);
	asctime_remove_nl[24] = 0;

	s->SendBytes("HTTP/1.1 ");

	if (! req.auth_realm_.empty() ) 
	{
		s->SendLine("401 Unauthorized");
		s->SendBytes("WWW-Authenticate: Basic Realm=\"");
		s->SendBytes(req.auth_realm_);
		s->SendLine("\"");
	}
	else 
	{
		s->SendLine(req.status_);
	}
	s->SendLine(std::string("Date: ") + asctime_remove_nl + " GMT");
	s->SendLine(std::string("Server: ") + serverName);
	s->SendLine("Connection: close");
	s->SendLine("Content-Type: text/html; charset=ISO-8859-1");
	s->SendLine("Content-Length: " + str_str.str());
	s->SendLine("");
	s->SendLine(req.answer_);

	s->Close( true ); // true = wait for all data to be sent before closing
	delete s;


	return 0;
}