예제 #1
0
파일: http.c 프로젝트: 340211173/httptunnel
ssize_t
http_write_request (int fd, Http_request *request)
{
  char str[1024]; /* FIXME: buffer overflow */
  ssize_t n = 0;
  size_t m;
  
  m = sprintf (str, "%s %s HTTP/%d.%d\r\n",
	       http_method_to_string (request->method),
	       request->uri,
	       request->major_version,
	       request->minor_version);
  m = write_all (fd, str, m);
  log_verbose ("http_write_request: %s", str);
  if (m == -1)
    {
      log_error ("http_write_request: write error: %s", strerror (errno));
      return -1;
    }
  n += m;

  m = http_write_header (fd, request->header);
  if (m == -1)
    {
      return -1;
    }
  n += m;

  return n;
}
예제 #2
0
파일: http.c 프로젝트: 340211173/httptunnel
static ssize_t
http_write_header (int fd, Http_header *header)
{
  ssize_t n = 0, m;

  if (header == NULL)
    return write_all (fd, "\r\n", 2);

  m = write_all (fd, (void *)header->name, strlen (header->name));
  if (m == -1)
    {
      return -1;
    }
  n += m;

  m = write_all (fd, ": ", 2);
  if (m == -1)
    {
      return -1;
    }
  n += m;

  m = write_all (fd, (void *)header->value, strlen (header->value));
  if (m == -1)
    {
      return -1;
    }
  n += m;

  m = write_all (fd, "\r\n", 2);
  if (m == -1)
    {
      return -1;
    }
  n += m;

  m = http_write_header (fd, header->next);
  if (m == -1)
    {
      return -1;
    }
  n += m;

  return n;
}
예제 #3
0
파일: main.c 프로젝트: HISONA/https_client
int main(int argc, char *argv[])
{
    char *url;
    char data[1024], response[4096];
    int  i, ret, size;

    HTTP_INFO hi1, hi2;


    // Init http session. verify: check the server CA cert.
    http_init(&hi1, FALSE);
    http_init(&hi2, TRUE);

/*
    url = "https://localhost:8080/upload";
    sprintf(data,
            "--1234567890abcdef\r\n"
            "Content-Disposition: form-data; name=\"upload\"; filename=\"test.txt\"\r\n"
            "Content-Type: text/plain\r\n\r\n"
            "test message\r\n"
            "--1234567890abcdef--\r\n\r\n"
    );

    ret = http_post(&hi1, url, data, response, sizeof(response));

    printf("return code: %d \n", ret);
    printf("return body: %s \n", response);
*/

    url = "https://localhost:8080/upload";

    if(http_open(&hi1, url) < 0)
    {
        http_strerror(data, 1024);
        printf("socket error: %s \n", data);

        goto error;
    }

    snprintf(hi1.request.method, 8, "POST");
    hi1.request.close = FALSE;
    hi1.request.chunked = FALSE;
    snprintf(hi1.request.content_type, 256, "multipart/form-data; boundary=1234567890abcdef");

    size = sprintf(data,
                   "--1234567890abcdef\r\n"
                   "Content-Disposition: form-data; name=\"upload\"; filename=\"test.txt\"\r\n"
                   "Content-Type: text/plain\r\n\r\n"
                   "test message\r\n"
                   "--1234567890abcdef--\r\n"
                   );

    hi1.request.content_length = size;

    if(http_write_header(&hi1) < 0)
    {
        http_strerror(data, 1024);
        printf("socket error: %s \n", data);

        goto error;
    }

    if(http_write(&hi1, data, size) != size)
    {
        http_strerror(data, 1024);
        printf("socket error: %s \n", data);

        goto error;
    }

    // Write end-chunked
    if(http_write_end(&hi1) < 0)
    {
        http_strerror(data, 1024);
        printf("socket error: %s \n", data);

        goto error;
    }

    ret = http_read_chunked(&hi1, response, sizeof(response));

    printf("return code: %d \n", ret);
    printf("return body: %s \n", response);


/*
    // Test a http get method.
    url = "http://httpbin.org/get?message=https_client";

    ret = http_get(&hi1, url, response, sizeof(response));

    printf("return code: %d \n", ret);
    printf("return body: %s \n", response);

    // Test a http post method.

    url = "http://httpbin.org/post";
    sprintf(data, "{\"message\":\"Hello, https_client!\"}");

    ret = http_post(&hi1, url, data, response, sizeof(response));

    printf("return code: %d \n", ret);
    printf("return body: %s \n", response);

    // Test a https get method.

    url = "https://httpbin.org/get?message=https_client";

    ret = http_get(&hi2, url, response, sizeof(response));

    printf("return code: %d \n", ret);
    printf("return body: %s \n", response);

    // Test a https post method.

    url = "https://httpbin.org/post";
    sprintf(data, "{\"message\":\"Hello, https_client!\"}");

    ret = http_post(&hi2, url, data, response, sizeof(response));

    printf("return code: %d \n", ret);
    printf("return body: %s \n", response);

    // Test a https post with the chunked-encoding data.

    url = "https://httpbin.org/post";

    if(http_open_chunked(&hi2, url) == 0)
    {
        size = sprintf(data, "[{\"message\":\"Hello, https_client %d\"},", 0);

        if(http_write_chunked(&hi2, data, size) != size)
        {
            http_strerror(data, 1024);
            printf("socket error: %s \n", data);

            goto error;
        }

        for(i=1; i<4; i++)
        {
            size = sprintf(data, "{\"message\":\"Hello, https_client %d\"},", i);
            if(http_write_chunked(&hi2, data, size) != size)
            {
                http_strerror(data, 1024);
                printf("socket error: %s \n", data);

                goto error;
            }
        }

        size = sprintf(data, "{\"message\":\"Hello, https_client %d\"}]", i);
        if(http_write_chunked(&hi2, data, strlen(data)) != size)
        {
            http_strerror(data, 1024);
            printf("socket error: %s \n", data);

            goto error;
        }

        ret = http_read_chunked(&hi2, response, sizeof(response));

        printf("return code: %d \n", ret);
        printf("return body: %s \n", response);

    }
    else
    {
        http_strerror(data, 1024);
        printf("socket error: %s \n", data);
    }

    error:
*/

error:

    http_close(&hi1);
    http_close(&hi2);

    return 0;
}