/*  httpget.cpp
    A simple http client

    13.08.2001: tk, initial implementation, based on Beck
                    example code.

    Copyright (c)2000 by Thomas Kindler, thomas.kindler@gmx.de

    This program is free software; you can redistribute it and/or
    modify it under the terms of the GNU General Public License as
    published by the Free Software Foundation; either version 2 of
    the License, or (at your option) any later version. Read the
    full License at http://www.gnu.org/copyleft for more details.
*/

// include files ----------
//
#include <conio.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <assert.h>
#include "inet/httpget.h"
#include "inet/base64.h"
#include "inet/dns.h"
#include "inet/url.h"
#include "sc12/tcpip.h"
#include "vs1001.cpp"
#include "misc.h"

int http_get(const char *urlString, char *result, unsigned maxlen)
{
  URL  *url = parse_url(urlString);

  unsigned long  ipnumber, ttl;
  char           ipaddr[16];

  if (inet_addr(url->host, &ipnumber)) {
    int ret = gethostbyname(NULL, url->host, &ttl, &ipnumber, ipaddr);
    if (!ret)
      printf("can't resolve ip address!\n");
  } else {
    strcpy( ipaddr, url->host );
  }

  printf("resolved to %s.\n", ipaddr);

  char *authcode = NULL;
  if (url->user) {
    char str[64];
    char *s = str;

    s += sprintf(s, "%s", url->user);
    if (url->pass)
      s += sprintf(s, ":%s", url->pass);

    assert((s-str)<sizeof(str));

    printf("str=\"%s\"", str);

    authcode = base64encode(str, strlen(str), NULL);
  }

  if (!url->port)
    url->port = 80;

  char  request[512];  // worst case !
  char *r = request;

  r += sprintf(r,
    "GET /%s HTTP/1.1\r\n" 
    "Host: %s:%u\r\n"
    "User-Agent: mp3v2 client 1.1, visit www.kreapc.de\r\n"
    "Connection: Keep-Alive\r\n",
    url->path ? url->path : "",
    url->host, url->port
  );
  if (authcode)
    r += sprintf(r, "Authorization: Basic %s\r\n", authcode);
  r += sprintf(r, "\r\n");

  assert((r-request) < sizeof(request));

  printf("request --------\n%s", request);

  int  error;
  int  sd;
  char *buf1, *buf2;
  unsigned rcvd;
  char *endheader;
  unsigned long t1, t2, t, total=0;

  sd = tcp_connect(ipaddr, 0, url->port, &error);
  if (error) {
    printf("error %d while connecting to \"%s\", port %u.\n", error, ipaddr, url->port);
    return 0;
  }

  send(sd, request, strlen(request), 0, &error);
  if (error) {
    printf( "error %d while sending request.\n", error );
    return 0;
    //    goto EndTransfer;
  }

  buf1 = &result[    0];
  buf2 = &result[32768];

  VS_Reset();

  rcvd = recv(sd, result, 1024, MSG_TIMEOUT, 10000L, &error);
  endheader = strstr(result, "\r\n\r\n");

  if (endheader) {
    *endheader = '\0';
    printf("Header:\n%s",result);
    endheader += 4;
    rcvd -= (int)(endheader-result);
    VS_PlayMpegAsync(endheader, rcvd, NULL, 0 );
  }

  while (!kbhit() && !error) {
    rcvd = 0;
    while (rcvd < 11616*2 && !error)
      rcvd += recv(sd, &buf1[rcvd], 4096, MSG_TIMEOUT, 10000L, &error);
    total += rcvd;
    printf("%d bytes.\n", rcvd);

    while (VS_IsPlaying());
    VS_PlayMpegAsync(buf1, rcvd, NULL, 0);
    rcvd = 0;
    while (rcvd < 11616*2 && !error)
      rcvd += recv(sd, &buf2[rcvd], 4096, MSG_TIMEOUT, 10000L, &error);
    printf("%d bytes.\n", rcvd);
    while (VS_IsPlaying());
    VS_PlayMpegAsync(buf2, rcvd, NULL, 0);
  }

EndTransfer:
  closesocket(sd, &error);
  if (error)
    printf( "error %d while closing connection to \"%s\", port %u.\n", error, ipaddr, url->port );


  return 0;
}
