Beispiel #1
0
static int
_pilot_servicehttp_runparser(struct pilot_service *thiz, ServerData *data)
{
	int len;
	switch (data->state)
	{
		case EBegin:
			data->req = httprequest_new();
			data->state = EParse;
		break;
		case EParse:
		{
			char buff[256];
			len = sizeof(buff);

			len = thiz->socket->action.read(thiz->socket, buff, len - 1);
			if (len < 0)
				data->state = EEnd;
			len = httprequest_parse(data->req, buff, len);
			if (len == 0)
				data->state = EContent;
		}
		break;
		case EContent:
		{
			Content *content = contentfile_new("/home/http", data->req->m_filepath);
			data->response = httpresponse_new(content);
			data->state = EResponse;
		}
		break;
		case EResponse:
		{
			char buff[256];
			len = sizeof(buff);
			len = httpresponse_format(data->response, buff, len - 1);
			if (len > 0)
				len = thiz->socket->action.write(thiz->socket, buff, len);
			else if (len == 0)
				data->state = EEnd;
		}
		break;
		case EEnd:
			if (data->response->m_Content)
				content_destroy(data->response->m_Content);
			data->response->m_Content = NULL;
			if (data->response)
				httpresponse_destroy(data->response);
			data->response = NULL;
			if (data->req)
				httprequest_destroy(data->req);
			data->req = NULL;
			data->state = EBegin;
		break;
	}
			
	return 0;
}
Beispiel #2
0
static void download_something()
{
  // important: the buffer is re-used all over again and prevents allocations
  HttpRequest* req = httprequest_create("localhost", 80, "/foo/bar/filename.zip", "GET", 256*1024);
  if (req)
  {
    httprequest_execute(req);
    const char* content = httprequest_get_content(req);
    size_t size = strtoul(httprequest_get_header(req,"Content-Length:"),0,0);
    if (content && size)
    {
      // OK
    }
    // frees the buffer, closes the connection
    httprequest_destroy(req);
  }
}