main.cpp 3.46 KB
#include "webserver.h"
#include "socket/src/Socket.h"
#include "nlohmann/json.hpp"
#include <sstream>



std::string urlDecode(std::string const &e_string) {
	std::string ret;
	int j;
	for (int i = 0; i < int(e_string.length()); i++) {
		if (int(e_string[i]) == 37) {
			sscanf_s(e_string.substr(i + 1, 2).c_str(), "%x", &j);
			const auto ch = static_cast<char>(j);
			ret += ch;
			i = i + 2;
		}
		else {
			ret += e_string[i];
		}
	}
	return (ret);
}

void Request_Handler(webserver::http_request* r) {
  Socket s = *(r->s_);
  nlohmann::json data;


  r->content_type_ = "application/json";
  if(r->path_=="/")
  {
	  r->status_ = "403 Forbidden";
	  data = { {"type","error"},{"method",r->method_},{"path",r->path_}};
  }
  else if (r->path_ == "/music")
  {
	  if (r->method_ == "GET") {}
	  else if (r->method_ == "POST") {}
	  else if (r->method_ == "DELETE") {}
	  else
	  {
		  r->status_ = "405 Method Not Allowed";
		  data = { {"type","error"},{"method",r->method_},{"path",r->path_} };;
	  }
  }
  else if (r->path_ == "/artist")
  {
	  if (r->method_ == "GET") {}
	  else if (r->method_ == "POST") {}
	  else if (r->method_ == "DELETE") {}
	  else
	  {
		  r->status_ = "405 Method Not Allowed";
		  data = { {"type","error"},{"method",r->method_},{"path",r->path_} };;
	  }
  }
  else if (r->path_ == "/album")
  {
	  if (r->method_ == "GET") {}
	  else if (r->method_ == "POST") {}
	  else if (r->method_ == "DELETE") {}
	  else
	  {
		  r->status_ = "405 Method Not Allowed";
		  data = { {"type","error"},{"method",r->method_},{"path",r->path_} };;
	  }
  }
  else if (r->path_ == "/genre")
  {
	  if (r->method_ == "GET") {}
	  else if (r->method_ == "POST") {}
	  else if (r->method_ == "DELETE") {}
	  else
	  {
		  r->status_ = "405 Method Not Allowed";
		  data = { {"type","error"},{"method",r->method_},{"path",r->path_} };;
	  }
  }
  else if (r->path_ == "/recent_play")
  {
	  if (r->method_ == "GET") {}
	  else if (r->method_ == "POST") {}
	  else if (r->method_ == "DELETE") {}
	  else
	  {
		  r->status_ = "405 Method Not Allowed";
		  data = { {"type","error"},{"method",r->method_},{"path",r->path_} };;
	  }
  }
  else if (r->path_ == "/recent_add")
  {
	  if (r->method_ == "GET") {}
	  else if (r->method_ == "POST") {}
	  else if (r->method_ == "DELETE") {}
	  else
	  {
		  r->status_ = "405 Method Not Allowed";
		  data = { {"type","error"},{"method",r->method_},{"path",r->path_} };;
	  }
  }
  else if (r->path_ == "/playlist")
  {
	  if (r->method_ == "GET") {}
	  else if (r->method_ == "POST") {}
	  else if (r->method_ == "DELETE") {}
	  else
	  {
		  r->status_ = "405 Method Not Allowed";
		  data = { {"type","error"},{"method",r->method_},{"path",r->path_} };
	  }
  }
  else if (r->path_ == "/search")
  {
	  const auto& query = r->params_;
	  if (r->method_ == "GET")
	  {
		   auto type_it=query.find("type");
		   auto value_it = query.find("value");
		  if(type_it==r->params_.end()||value_it == r->params_.end())
		  {
			  r->status_ = "400 Bad Request";
			  data = { {"type","error"},{"method",r->method_},{"path",r->path_} };;
		  }
		  else
		  {
			  
		  }
	  }
	  else
	  {
		  r->status_ = "405 Method Not Allowed";
		  data = { {"type","error"},{"method",r->method_},{"path",r->path_} };;
	  }
  }
  else{
	  r->status_ = "404 Not Found";
	  data = { {"type","error"},{"method",r->method_},{"path",r->path_} };
  }
	nlohmann::json result = {
		{"status", r->status_},
		{"data", data}
	};
	r->answer_ = result.dump();
}

int main() {
  webserver(8080, Request_Handler);
}