Exemple #1
0
//invoked by tinyweb when GET request comes in
//please invoke write_uv_data() once and only once on every request, to send respone to client and close the connection.
//if not handle this request (by invoking write_uv_data()), you can close connection using tinyweb_close_client(client).
//pathinfo: "/" or "/book/view/1"
//query_stirng: the string after '?' in url, such as "id=0&value=123", maybe NULL or ""
static void tinyweb_on_request_get(uv_stream_t* client, const char* pathinfo, const char* query_stirng) {
	//request a file?
	char* postfix = strrchr(pathinfo, '.');
	if(postfix) {
		postfix++;
		if(_doc_root_path) {
			char file[1024];
			snprintf(file, sizeof(file), "%s%s", _doc_root_path, pathinfo);
			_on_send_file(client, _get_content_type(postfix), file, pathinfo);
			return;
		} else {
			_404_not_found(client, pathinfo);
			return;
		}
	}

	//request an action
	if(strcmp(pathinfo, "/") == 0) {
		char* respone = format_http_respone("200 OK", "text/html", "Welcome to tinyweb. <a href=\"index.html\">index.html</a>", -1, NULL);
		write_uv_data(client, respone, -1, 0, 1);
	} else if(strcmp(pathinfo, "/404") == 0) {
		char* respone = format_http_respone("404 Not Found", "text/html", "<h3>404 Page Not Found<h3>", -1, NULL);
		write_uv_data(client, respone, -1, 0, 1);
	} else {
		char* respone;
		char content[1024];
		snprintf(content, sizeof(content), "<h1>tinyweb</h1><p>pathinfo: %s</p><p>query stirng: %s</p>", pathinfo, query_stirng);
		respone = format_http_respone("200 OK", "text/html", content, -1, NULL);
		write_uv_data(client, respone, -1, 0, 1);
	}
}
Exemple #2
0
int8_t
_read_header(char * buf, struct request *req)
{
  int8_t         error;

  error = 0; /* no error */

  if ((req->method = _get_request_method(buf)) == MPOST) {
    error += _get_content_length(buf, req);
    error += _get_content_type(buf, req);
  }
  (void) _get_request_uri(buf, req);
  (void) _get_protocol_version(buf, req);

  return error;
}