예제 #1
0
/*---------------------------------------------------------------------------*/
static void
event(struct tcp_socket *tcps, void *ptr,
      tcp_socket_event_t e)
{
  struct http_socket *s = ptr;
  char host[MAX_HOSTLEN];
  char path[MAX_PATHLEN];
  uint16_t port;
  char str[42];
  int len;

  if(e == TCP_SOCKET_CONNECTED) {
    puts("Connected");
    if(parse_url(s->url, host, &port, path)) {
      tcp_socket_send_str(tcps, s->postdata != NULL ? "POST " : "GET ");
      tcp_socket_send_str(tcps, path);
      tcp_socket_send_str(tcps, " HTTP/1.1\r\n\r");
      tcp_socket_send_str(tcps, "Connection: close\r\n\r");
      tcp_socket_send_str(tcps, "Host: ");
      tcp_socket_send_str(tcps, host);
      tcp_socket_send_str(tcps, "\r\n\r");
      if(s->postdata != NULL) {
        if(s->content_type) {
          tcp_socket_send_str(tcps, "Content-Type: ");
          tcp_socket_send_str(tcps, s->content_type);
          tcp_socket_send_str(tcps, "\r\n\r");
        }
        tcp_socket_send_str(tcps, "Content-Length: ");
        sprintf(str, "%u", s->postdatalen);
        tcp_socket_send_str(tcps, str);
        tcp_socket_send_str(tcps, "\r\n\r");
      } else if(s->length || s->pos > 0) {
        tcp_socket_send_str(tcps, "Range: bytes=");
        if(s->length) {
          if(s->pos >= 0) {
            sprintf(str, "%llu-%llu", s->pos, s->pos + s->length - 1);
          } else {
            sprintf(str, "-%llu", s->length);
          }
        } else {
          sprintf(str, "%llu-", s->pos);
        }
        tcp_socket_send_str(tcps, str);
        tcp_socket_send_str(tcps, "\r\n\r");
      }
      tcp_socket_send_str(tcps, "\r\n\r");
      if(s->postdata != NULL && s->postdatalen) {
        len = tcp_socket_send(tcps, s->postdata, s->postdatalen);
        s->postdata += len;
        s->postdatalen -= len;
      }
    }
    parse_header_init(s);
  } else if(e == TCP_SOCKET_CLOSED) {
    call_callback(s, HTTP_SOCKET_CLOSED, NULL, 0);
    removesocket(s);
    puts("Closed");
  } else if(e == TCP_SOCKET_TIMEDOUT) {
    call_callback(s, HTTP_SOCKET_TIMEDOUT, NULL, 0);
    removesocket(s);
    puts("Timedout");
  } else if(e == TCP_SOCKET_ABORTED) {
    call_callback(s, HTTP_SOCKET_ABORTED, NULL, 0);
    removesocket(s);
    puts("Aborted");
  } else if(e == TCP_SOCKET_DATA_SENT) {
    if(s->postdata != NULL && s->postdatalen) {
      len = tcp_socket_send(tcps, s->postdata, s->postdatalen);
      s->postdata += len;
      s->postdatalen -= len;
    } else {
      start_timeout_timer(s);
    }
  }
}
예제 #2
0
/*---------------------------------------------------------------------------*/
static void
event(struct tcp_socket *tcps, void *ptr,
      tcp_socket_event_t e)
{
  struct http_socket *s = ptr;
  char host[MAX_HOSTLEN];
  char path[MAX_PATHLEN];
  char content_lenstr[8];
  uint16_t port;

  if(e == TCP_SOCKET_CONNECTED) {
    PRINTF("Connected\n");
    if(parse_url(s->url, host, &port, path)) {

      tcp_socket_send_str(tcps, HTTP_METHOD_STR(s->method));
      tcp_socket_send_str(tcps, " ");
      tcp_socket_send_str(tcps, path);
      tcp_socket_send_str(tcps, " HTTP/1.0\r\n");

      tcp_socket_send_str(tcps, "Host: ");
      tcp_socket_send_str(tcps, host);
      tcp_socket_send_str(tcps, "\r\n");

      if (s->method == HTTP_GET || s->method == HTTP_HEAD || s->method == HTTP_DELETE) {
        tcp_socket_send_str(tcps, "\r\n");
      }
    }
    parse_header_init(s);
  } else if(e == TCP_SOCKET_DATA_SENT) {
    if (s->method == HTTP_POST) {
      if (s->post_headers_sent == 0) {
        if (s->connection_policy == HTTP_CONNECTION_KEEPALIVE) {
          tcp_socket_send_str(tcps, "Connection: Keep-Alive\r\n");
        }
        tcp_socket_send_str(tcps, "Content-type: ");
        tcp_socket_send_str(tcps, s->content_type);
        tcp_socket_send_str(tcps, "\r\n");

        snprintf(content_lenstr, sizeof(content_lenstr), "%d", s->request_datalen);
        tcp_socket_send_str(tcps, "Content-length: ");
        tcp_socket_send_str(tcps, content_lenstr);
        tcp_socket_send_str(tcps, "\r\n");
        tcp_socket_send_str(tcps, "\r\n");

        s->post_headers_sent = 1;
      }
      if (s->request_dataptr < s->request_datalen) {
        s->request_dataptr += tcp_socket_send(tcps, &s->request_data[s->request_dataptr], s->request_datalen - s->request_dataptr);
      }
    }
  } else if(e == TCP_SOCKET_CLOSED) {
    call_callback(s, HTTP_SOCKET_CLOSED, NULL, 0);
    removesocket(s);
    PRINTF("Closed\n");
  } else if(e == TCP_SOCKET_TIMEDOUT) {
    call_callback(s, HTTP_SOCKET_TIMEDOUT, NULL, 0);
    removesocket(s);
    PRINTF("Timedout\n");
  } else if(e == TCP_SOCKET_ABORTED) {
    call_callback(s, HTTP_SOCKET_ABORTED, NULL, 0);
    removesocket(s);
    PRINTF("Aborted\n");
  }
}
예제 #3
0
/*---------------------------------------------------------------------------*/
static void
event(struct tcp_socket *tcps, void *ptr,
      tcp_socket_event_t e)
{
  struct http_socket *s = ptr;
  char host[MAX_HOSTLEN];
  char path[MAX_PATHLEN];
  uint16_t port;
  char str[42];
  int len;

  if(e == TCP_SOCKET_CONNECTED) {
    printf("Connected\n");
    if(parse_url(s->url, host, &port, path)) {
      tcp_socket_send_str(tcps, s->postdata != NULL ? "POST " : "GET ");
      if(s->proxy_port != 0) {
        /* If we are configured to route through a proxy, we should
           provide the full URL as the path. */
        tcp_socket_send_str(tcps, s->url);
      } else {
        tcp_socket_send_str(tcps, path);
      }
      tcp_socket_send_str(tcps, " HTTP/1.1\r\n");
      tcp_socket_send_str(tcps, "Connection: close\r\n");
      tcp_socket_send_str(tcps, "Host: ");
      /* If we have IPv6 host, add the '[' and the ']' characters
         to the host. As in rfc2732. */
      if(memchr(host, ':', MAX_HOSTLEN)) {
        tcp_socket_send_str(tcps, "[");
      }
      tcp_socket_send_str(tcps, host);
      if(memchr(host, ':', MAX_HOSTLEN)) {
        tcp_socket_send_str(tcps, "]");
      }
      tcp_socket_send_str(tcps, "\r\n");
      if(s->postdata != NULL) {
        if(s->content_type) {
          tcp_socket_send_str(tcps, "Content-Type: ");
          tcp_socket_send_str(tcps, s->content_type);
          tcp_socket_send_str(tcps, "\r\n");
        }
        tcp_socket_send_str(tcps, "Content-Length: ");
        sprintf(str, "%u", s->postdatalen);
        tcp_socket_send_str(tcps, str);
        tcp_socket_send_str(tcps, "\r\n");
      } else if(s->length || s->pos > 0) {
        tcp_socket_send_str(tcps, "Range: bytes=");
        if(s->length) {
          if(s->pos >= 0) {
            sprintf(str, "%llu-%llu", s->pos, s->pos + s->length - 1);
          } else {
            sprintf(str, "-%llu", s->length);
          }
        } else {
          sprintf(str, "%llu-", s->pos);
        }
        tcp_socket_send_str(tcps, str);
        tcp_socket_send_str(tcps, "\r\n");
      }
      tcp_socket_send_str(tcps, "\r\n");
      if(s->postdata != NULL && s->postdatalen) {
        len = tcp_socket_send(tcps, s->postdata, s->postdatalen);
        s->postdata += len;
        s->postdatalen -= len;
      }
    }
    parse_header_init(s);
  } else if(e == TCP_SOCKET_CLOSED) {
    call_callback(s, HTTP_SOCKET_CLOSED, NULL, 0);
    removesocket(s);
    printf("Closed\n");
  } else if(e == TCP_SOCKET_TIMEDOUT) {
    call_callback(s, HTTP_SOCKET_TIMEDOUT, NULL, 0);
    removesocket(s);
    printf("Timedout\n");
  } else if(e == TCP_SOCKET_ABORTED) {
    call_callback(s, HTTP_SOCKET_ABORTED, NULL, 0);
    removesocket(s);
    printf("Aborted\n");
  } else if(e == TCP_SOCKET_DATA_SENT) {
    if(s->postdata != NULL && s->postdatalen) {
      len = tcp_socket_send(tcps, s->postdata, s->postdatalen);
      s->postdata += len;
      s->postdatalen -= len;
    } else {
      start_timeout_timer(s);
    }
  }
}