コード例 #1
0
ファイル: nanohttp-server.c プロジェクト: ayang64/csoap
/**
  Send boundary and part header and continue 
  with next part
*/
herror_t
httpd_mime_next(httpd_conn_t * conn, const char *content_id,
                const char *content_type, const char *transfer_encoding)
{
  herror_t status;
  char buffer[512];
  char boundary[75];
  int len;

  /* Get the boundary string */
  _httpd_mime_get_boundary(conn, boundary);
  len = sprintf(buffer, "\r\n--%s\r\n", boundary);

  /* Send boundary */
  if ((status = http_output_stream_write(conn->out, buffer, len)) != H_OK)
    return status;

  /* Send Content header */
  len = sprintf(buffer, "%s: %s\r\n%s: %s\r\n%s: %s\r\n\r\n",
            HEADER_CONTENT_TYPE, content_type ? content_type : "text/plain",
            HEADER_CONTENT_TRANSFER_ENCODING,
            transfer_encoding ? transfer_encoding : "binary",
            HEADER_CONTENT_ID,
            content_id ? content_id : "<content-id-not-set>");

  return http_output_stream_write(conn->out, buffer, len);
}
herror_t
httpc_mime_next(httpc_conn_t * conn,
                const char *content_id,
                const char *content_type, const char *transfer_encoding)
{
  herror_t status;
  char buffer[512];
  char boundary[75];

  /* Get the boundary string */
  _httpc_mime_get_boundary(conn, boundary);
  sprintf(buffer, "\r\n--%s\r\n", boundary);

  /* Send boundary */
  status = http_output_stream_write(conn->out,
                                    (const byte_t *) buffer, strlen(buffer));

  if (status != H_OK)
    return status;

  /* Send Content header */
  sprintf(buffer, "%s: %s\r\n%s: %s\r\n%s: %s\r\n\r\n",
          HEADER_CONTENT_TYPE, content_type,
          HEADER_CONTENT_TRANSFER_ENCODING, transfer_encoding,
          HEADER_CONTENT_ID, content_id);

  return http_output_stream_write(conn->out,
                                    (const byte_t *) buffer, strlen(buffer));
}
コード例 #3
0
ファイル: http_server.c プロジェクト: ayang64/csoap
static void
headers_service(httpd_conn_t *conn, struct hrequest_t *req)
{
  hpair_t *walker;
  char buf[512];
  int len;

  httpd_send_header(conn, 200, HTTP_STATUS_200_REASON_PHRASE);
  http_output_stream_write_string(conn->out,
    "<html>"
      "<head>"
        "<title>Request headers</title>"
      "</head>"
      "<body>"
        "<h1>Request headers</h1>"
        "<ul>");

  for (walker=req->header; walker; walker=walker->next)
  {
    len = snprintf(buf, 512, "<li>%s: %s</li>", walker->key, walker->value);
    http_output_stream_write(conn->out, buf, len);
  }

  http_output_stream_write_string(conn->out,
        "</ul>"
      "</body>"
    "</html>");
}
コード例 #4
0
ファイル: nanohttp-server.c プロジェクト: ayang64/csoap
static herror_t
_httpd_send_html_message(httpd_conn_t *conn, int reason, const char *phrase, const char *msg)
{
  const char const *tmpl =
    "<html>"
      "<head>"
        "<title>%s</title>"
      "</head>"
      "<body>"
        "<h3>%s</h3>"
	"<hr/>"
        "<div>Message: '%s'</div>"
      "</body>"
    "</html>";
  char buf[4096];
  char slen[5];
  int len;

  len = snprintf(buf, 4096, tmpl, phrase, phrase, msg);
  snprintf(slen, 5, "%d", len);

  httpd_set_header(conn, HEADER_CONTENT_LENGTH, slen);
  httpd_send_header(conn, reason, phrase);

  return http_output_stream_write(conn->out, buf, len);
}
herror_t
httpc_mime_end(httpc_conn_t * conn, hresponse_t ** out)
{
  herror_t status;
  char buffer[512];
  char boundary[75];

  /* Get the boundary string */
  _httpc_mime_get_boundary(conn, boundary);
  sprintf(buffer, "\r\n--%s--\r\n\r\n", boundary);

  /* Send boundary */
  status = http_output_stream_write(conn->out,
                                    (const byte_t *) buffer, strlen(buffer));

  if (status != H_OK)
    return status;

  if ((status = http_output_stream_flush(conn->out)) != H_OK)
    return status;

  if ((status = hresponse_new_from_socket(&(conn->sock), out)) != H_OK)
    return status;

  return H_OK;
}
コード例 #6
0
ファイル: http_server.c プロジェクト: ayang64/csoap
static void
post_service(httpd_conn_t *conn, struct hrequest_t *req)
{
  if (req->method == HTTP_REQUEST_POST)
  {
    unsigned char buffer[1024];
    long len, total;

    httpd_send_header(conn, 200, HTTP_STATUS_200_REASON_PHRASE);
    http_output_stream_write_string(conn->out,
      "<html>"
        "<head>"
	  "<title>POST service</title>"
        "</head>"
        "<body>"
          "<h1>You posted</h1>"
          "<pre>");

    if (req->content_type && req->content_type->type)
    {
      len = sprintf(buffer, "<p>Content-Type: %s</p>", req->content_type->type);
      http_output_stream_write(conn->out, buffer, len);
    }

    while (http_input_stream_is_ready(req->in))
    {
	    len = http_input_stream_read(req->in, buffer, 1024);
	    http_output_stream_write(conn->out, buffer, len);
	    total += len;
    }

    http_output_stream_write_string(conn->out,
          "</pre>");

    len = sprintf(buffer, "<p>Received %li bytes</p>", total);
    http_output_stream_write(conn->out, buffer, len);

    http_output_stream_write_string(conn->out,
        "</body>"
      "</html>");
  }
  else
  {
    httpd_send_not_implemented(conn, "post_service");
  }
}
/**
  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;
}
コード例 #8
0
ファイル: nanohttp-server.c プロジェクト: ayang64/csoap
/**
  Finish MIME request 
  Returns: H_OK  or error flag
*/
herror_t
httpd_mime_end(httpd_conn_t * conn)
{
  herror_t status;
  char buffer[512];
  char boundary[75];
  int len;

  /* Get the boundary string */
  _httpd_mime_get_boundary(conn, boundary);
  len = sprintf(buffer, "\r\n--%s--\r\n\r\n", boundary);

  /* Send boundary */
  if ((status = http_output_stream_write(conn->out, buffer, len)) != H_OK)
    return status;

  return http_output_stream_flush(conn->out);
}
コード例 #9
0
ファイル: nanohttp-server.c プロジェクト: ayang64/csoap
/**
  Send boundary and part header and continue 
  with next part
*/
herror_t
httpd_mime_send_file(httpd_conn_t * conn, const char *content_id,
                     const char *content_type, const char *transfer_encoding,
                     const char *filename)
{
  unsigned char buffer[MAX_FILE_BUFFER_SIZE];
  herror_t status;
  FILE *fd;
  size_t size;

  if ((fd = fopen(filename, "rb")) == NULL)
    return herror_new("httpd_mime_send_file", FILE_ERROR_OPEN,
                      "Can not open file '%d'", filename);

  status = httpd_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("httpd_mime_send_file", FILE_ERROR_READ,
                        "Can not read from file '%d'", filename);
    }

    if ((status = http_output_stream_write(conn->out, buffer, size)) != H_OK)
    {
      fclose(fd);
      return status;
    }
  }

  fclose(fd);
  return H_OK;
}
コード例 #10
0
ファイル: mime_client.c プロジェクト: ayang64/csoap
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;
}