Пример #1
0
// called by download thread itself if download was canceled
Download::~Download()
{
	if (http && (keepAlive || downloadCanceled))
		http_async_req_close(http);
	if (downloadData)
		free(downloadData);
}
Пример #2
0
char *http_async_req_stop(void *ctx, int *ret, int *len)
{
  struct http_ctx *cx = ctx;
  char *rxd;

  if(cx->state != HTS_DONE)
    while(!http_async_req_status(ctx)) ;

  if(cx->host) {
    free(cx->host);
    cx->host = NULL;
  }
  if(cx->path) {
    free(cx->path);
    cx->path = NULL;
  }
  if(cx->txd) {
    free(cx->txd);
    cx->txd = NULL;
    cx->txdl = 0;
  }
  if(cx->hbuf) {
    free(cx->hbuf);
    cx->hbuf = NULL;
  }
  if(cx->thdr) {
    free(cx->thdr);
    cx->thdr = NULL;
    cx->thlen = 0;
  }

  if(ret)
    *ret = cx->ret;
  if(len)
    *len = cx->rptr;
  if(cx->rbuf)
    cx->rbuf[cx->rptr] = 0;
  rxd = cx->rbuf;
  cx->rbuf = NULL;
  cx->rlen = 0;
  cx->rptr = 0;
  cx->contlen = 0;

  if(!cx->keep)
    http_async_req_close(ctx);
  else if(cx->cclose) {
    PCLOSE(cx->fd);
    cx->fd = PERROR;
    if(cx->fdhost) {
      free(cx->fdhost);
      cx->fdhost = NULL;
    }
    cx->state = HTS_STRT;
  } else
    cx->state = HTS_IDLE;

  return rxd;
}
Пример #3
0
void *http_async_req_start(void *ctx, const char *uri, const char *data, int dlen, int keep)
{
	struct http_ctx *cx = (http_ctx *)ctx;
	if (cx && time(NULL) - cx->last > http_timeout)
	{
		http_force_close(ctx);
		http_async_req_close(ctx);
		ctx = NULL;
	}
	if (!ctx)
	{
		ctx = calloc(1, sizeof(struct http_ctx));
		cx = (http_ctx *)ctx;
		cx->fd = PERROR;
	}

	if (!cx->hbuf)
	{
		cx->hbuf = (char *)malloc(256);
		cx->hlen = 256;
	}

	if (!http_up)
	{
		cx->ret = 604;
		cx->state = HTS_DONE;
		return ctx;
	}

	if (cx->state!=HTS_STRT && cx->state!=HTS_IDLE)
	{
		fprintf(stderr, "HTTP: unclean request restart state.\n");
		exit(1);
	}

	cx->keep = keep;
	cx->ret = 600;
	if (splituri(uri, &cx->host, &cx->path))
	{
		cx->ret = 601;
		cx->state = HTS_DONE;
		return ctx;
	}
	if (http_use_proxy)
	{
		free(cx->path);
		cx->path = mystrdup(uri);
	}
	if (cx->fdhost && strcmp(cx->host, cx->fdhost))
	{
		free(cx->fdhost);
		cx->fdhost = NULL;
		PCLOSE(cx->fd);
		cx->fd = PERROR;
		cx->state = HTS_STRT;
	}
	if (data)
	{
		if (!dlen)
			dlen = strlen(data);
		cx->txd = (char *)malloc(dlen);
		memcpy(cx->txd, data, dlen);
		cx->txdl = dlen;
	}
	else
		cx->txdl = 0;

	cx->contlen = 0;
	cx->chunked = 0;
	cx->chunkhdr = 0;
	cx->rxtogo = 0;
	cx->cclose = 0;

	cx->tptr = 0;
	cx->tlen = 0;

	cx->last = time(NULL);

	return ctx;
}