int httpd_wsgi()

in AZ3166/src/cores/arduino/httpserver/httpd_wsgi.c [618:710]


int httpd_wsgi(httpd_request_t *req_p)
{
	struct httpd_wsgi_call *f;
	int err = -WM_E_HTTPD_NO_HANDLER;
	int index, ret = 0;
	int match_index = -1, cur_char_match = 0;
	bool found;

	char *ptr, *request = httpd_skip_absolute_http_path(req_p->filename);

	httpd_d("httpd_wsgi: looking for %s", request);
        
	/* IMP: fixme: Need to check for ? at the end or any forward slashes /
	 * httpd_validate URI? */
	for (index = 0; index < MAX_WSGI_HANDLERS; index++) {
		f = all_wsgi_calls[index];
		if (f == NULL)
			continue;
		if (f->http_flags & APP_HTTP_FLAGS_NO_EXACT_MATCH) {
			ret = get_matching_chars(request,
						f->uri);
			if (ret > cur_char_match) {
				cur_char_match = ret;
				match_index = index;
			}
		} else {
			if (!strncmp(request, f->uri, strlen(f->uri))) {
				found = 0;
				ptr = request;
				ptr += strlen(f->uri);
				/* '?' terminates a filename */
				if (*ptr == '?')
					found = 1;
				else {
					/* Check for any number of
					 * forward slashes */
					while (*ptr && (*ptr == '/')) {
						ptr++;
					}
					if (!*ptr)
						found = 1;
				}
				if (found) {
					httpd_d("Anchored pattern match: %s",
						f->uri);
					match_index = index;
					/* Break if there is an exact match */
					break;
				}
			}
		}
	}
	if (match_index == -1)
		return err;

	/* Match found. So map the wsgi to this request */
	req_p->wsgi = all_wsgi_calls[match_index];
	switch (req_p->type) {
	case HTTPD_REQ_TYPE_HEAD:
	case HTTPD_REQ_TYPE_GET:
		if (all_wsgi_calls[match_index]->get_handler)
			err = all_wsgi_calls[match_index]->get_handler(req_p);
		else
			return err;
		break;
	case HTTPD_REQ_TYPE_POST:
		if (all_wsgi_calls[match_index]->set_handler)
			err = all_wsgi_calls[match_index]->set_handler(req_p);
		else
			return err;
		break;
	case HTTPD_REQ_TYPE_PUT:
		if (all_wsgi_calls[match_index]->put_handler)
			err = all_wsgi_calls[match_index]->put_handler(req_p);
		else
			return err;
		break;
	case HTTPD_REQ_TYPE_DELETE:
		if (all_wsgi_calls[match_index]->delete_handler)
			err = all_wsgi_calls[match_index]->delete_handler(req_p);
		else
			return err;
		break;
	default:
		return err;
	}

	if (err == kNoErr)
		return HTTPD_DONE;
	else
		return err;

}