/* from curl tool_cb_hdr.c, Copyright (C) 1998 - 2011, Daniel Stenberg */
static size_t fetch_header_cb(void *ptr, size_t size, size_t nmemb, void *userdata)
{
  const size_t cb = size * nmemb;
  const size_t failure = (cb) ? 0 : 1;
  fetch_curl_t *ft = userdata;
  char *temp;
  char *work;
  char *end;
  size_t len;

  updatecontext();
  if (ft == NULL)
    return cb; /* ignore */

#ifdef DEBUG
  if(size * nmemb > (size_t)CURL_MAX_HTTP_HEADER) {
    outerror(OUTERROR_TYPE_WARN_LOUD, "Header data = %ld exceeds single call write limit!", size);
    return failure;
  }
#endif
  temp = mystrdup(ptr);
  len = cb;
  if (temp[len - 1] == '\n') {
    --(len);
    if (temp[len - 1] == '\r') {
      --(len);
    }
  }
  temp[len] = 0;
  if ((gdata.debug > 0) && (cb > 2)) {
    a_respond(&(ft->u), "FETCH header '%s'", temp);
  }
  if (cb >= 20) {
    if (strncasecmp("Content-disposition:", temp, 20) == 0) { /* NOTRANSLATE */
      /* look for the 'filename=' parameter
         (encoded filenames (*=) are not supported) */
      work = strstr(temp + 20, "filename="); /* NOTRANSLATE */
      if (work != NULL) {
        work += 9;
        /* stop at first ; */
        end = strchr(work, ';');
        if (end != NULL)
          *end = 0;

        ft->contentname = mystrdup(work);
        clean_quotes(ft->contentname);
        a_respond(&(ft->u), "FETCH filename '%s'", ft->contentname);
      }
    }
  }

  mydelete(temp);
  return cb;
}
/* extract everything starting with the given argument */
char *getpart_eol(const char *line, unsigned int howmany)
{
  const char *start;
  const char *src;
  char *dest;
  size_t plen;
  unsigned int inquotes;
  unsigned int moreargs;
  unsigned int morequote;
  unsigned int part;

  if (line == NULL)
    return NULL;

  if (howmany <= 0)
    return NULL;

  inquotes = 0;
  moreargs = 0;
  morequote = 0;
  part = 0;

  start = line;
  for (src = start; ; ++src) {
    if ((*src == ' ') && (inquotes != 0))
      continue;
    if (*src == '"') {
      if ((start == src) && (inquotes == 0)) {
        ++inquotes;
        continue;
      }
      if (inquotes == 0)
        continue;
      --inquotes;
      if (part + 1 == howmany) {
        ++morequote;
        continue;
      }
      ++start;
    } else {
      if (*src) {
        if (*src != ' ')
          continue;
        if (src == start) {
          /* skip leading spaces */
          ++start;
          continue;
        }
        if (part + 1 == howmany) {
          ++moreargs;
          continue;
        }
      }
    }
    plen = src - start;
    if (plen == 0)
      continue;

   if (++part < howmany) {
      if (*src == 0)
        return NULL;
      start = src + 1;
      continue;
    }

    /* found end */
    break;
  }
  dest = mystrdup(start);
  if ((morequote > 0) && (moreargs == 0))
    clean_quotes(dest);
  return dest;
}
/* split a line in a number of arguments */
unsigned int get_argv(char **result, const char *line, unsigned int howmany)
#endif /* WITHOUT_MEMSAVE */
{
  const char *start;
  const char *src;
  char *dest;
  size_t plen;
  unsigned int inquotes;
  unsigned int moreargs;
  unsigned int morequote;
  unsigned int part;

  if (howmany == 0)
    return 0;

  if (line == NULL) {
    clean_missing_parts(result, 0, howmany);
    return 0;
  }

  inquotes = 0;
  moreargs = 0;
  morequote = 0;
  part = 0;

  start = line;
  for (src = start; ; ++src) {
    if ((*src == ' ') && (inquotes != 0))
      continue;
    if (*src == '"') {
      if ((start == src) && (inquotes == 0)) {
        ++inquotes;
        continue;
      }
      if (inquotes == 0)
        continue;
      --inquotes;
      if (part + 1 == howmany) {
        ++morequote;
        continue;
      }
      ++start;
    } else {
      if (*src) {
        if (*src != ' ')
          continue;
        if (src == start) {
          /* skip leading spaces */
          ++start;
          continue;
        }
        if (part + 1 == howmany) {
          ++moreargs;
          continue;
        }
      }
    }
    plen = src - start;
    if (plen == 0)
      continue;

    if (*src == '"')
      ++src;

    /* found end */
#ifndef WITHOUT_MEMSAVE
    dest = (char *)mymalloc2(plen + 1, 0, src_function, src_file, src_line);
#else /* WITHOUT_MEMSAVE */
    dest = mymalloc(plen + 1);
#endif /* WITHOUT_MEMSAVE */
    memcpy(dest, start, plen);
    dest[plen] = '\0';
    if ((morequote > 0) && (moreargs == 0))
      clean_quotes(dest);
    result[part++] = dest;
    if (part >= howmany)
      return part;
    if (*src == 0) {
      clean_missing_parts(result, part, howmany);
      return part;
    }
    start = src + 1;
  }
}