Esempio n. 1
0
redhttp_response_t *redhttp_server_dispatch_request(redhttp_server_t * server,
                                                    redhttp_request_t * request)
{
  redhttp_response_t *response = NULL;
  redhttp_handler_t *it;

  assert(server != NULL);
  assert(request != NULL);

  for (it = server->handlers; it; it = it->next) {
    if (match_route(it, request)) {
      response = it->func(request, it->user_data);
      if (response)
        return response;
    }
  }

  // Is it a HEAD request?
  if (strncmp("HEAD", request->method, 4) == 0) {
    for (it = server->handlers; it; it = it->next) {
      if ((it->method && strcmp(it->method, "GET") == 0) &&
          (it->path && strcmp(it->path, request->path) == 0)) {
        response = redhttp_response_new(REDHTTP_OK, NULL);
        break;
      }
    }

    if (!response)
      response = redhttp_response_new(REDHTTP_NOT_FOUND, NULL);

  } else {
    // Check if another method is allowed instead
    for (it = server->handlers; it; it = it->next) {
      if (it->path && strcmp(it->path, request->path) == 0) {
        response = redhttp_response_new_error_page(REDHTTP_METHOD_NOT_ALLOWED, NULL);
        // FIXME: add list of allowed methods
        break;
      }
    }
  }

  // Must be a 404
  if (!response)
    response = redhttp_response_new_error_page(REDHTTP_NOT_FOUND, NULL);

  return response;
}
Esempio n. 2
0
struct restgres_route *
find_best_route(struct restgres_request *req)
{
	struct restgres_route *best_route=NULL;
	int best_score = 0;
	int i;
	for(i=0; i < num_routes; i++)
	{
		struct restgres_route *route = &routes[i];
		int score = match_route(route, req);
		if(score > best_score)
		{
			best_route = route;
			best_score = score;
		}
	}
	return best_route;
}