/**
  Send boundary and part header and continue 
  with next part
*/
herror_t
httpc_mime_send_file(httpc_conn_t * conn,
                     const char *content_id,
                     const char *content_type,
                     const char *transfer_encoding, const char *filename)
{
  herror_t status;
  FILE *fd = fopen(filename, "rb");
  byte_t buffer[MAX_FILE_BUFFER_SIZE];
  size_t size;

  if (fd == NULL)
    return herror_new("httpc_mime_send_file", FILE_ERROR_OPEN,
                      "Can not open file '%s'", filename);

  status = httpc_mime_next(conn, content_id, content_type, transfer_encoding);
  if (status != H_OK)
  {
    fclose(fd);
    return status;
  }

  while (!feof(fd))
  {
    size = fread(buffer, 1, MAX_FILE_BUFFER_SIZE, fd);
    if (size == -1)
    {
      fclose(fd);
      return herror_new("httpc_mime_send_file", FILE_ERROR_READ,
                        "Can not read from file '%s'", filename);
    }

    if (size > 0)
    {
      /* DEBUG: fwrite(buffer, 1, size, stdout); */
      status = http_output_stream_write(conn->out, buffer, size);
      if (status != H_OK)
      {
        fclose(fd);
        return status;
      }
    }
  }

  fclose(fd);
  log_verbose1("file sent!");
  return H_OK;
}
Example #2
0
static
int send_file(httpc_conn_t *conn, const char* filename, const char* id, const char* content_type)
{
  herror_t status;
  int size;
  FILE *f = fopen(filename, "r");
  char buffer[MAX_BUFFER_SIZE];

  if ((f = fopen(filename, "r")) == NULL)
  {
    fprintf(stderr, "cannot open file: '%s' (%s)\n", filename, strerror(errno));
    return -1;
  }

  if ((status = httpc_mime_next(conn, id, content_type, "binary")) != H_OK)
  {
    fprintf(stderr, "httpc_mime_next failed (%s)\n", herror_message(status));
    herror_release(status);
    return -1;
  }
  
  while (!feof(f))
  {
    size = fread(buffer, 1, MAX_BUFFER_SIZE, f);
    if (size == -1)
    {
      fprintf(stderr, "Cannot read file (%s)\n", strerror(errno));
      fclose(f);
      return -1;
    }
    http_output_stream_write(conn->out, buffer, size);
  }

  fclose(f);
  return 0;
}