HTTPクライアント -- ヘッダとボディの分離

戻る

::::::::::::::
Makefile
::::::::::::::
# $Id: http2.html,v 1.1 2009/06/22 16:12:13 kishi Exp kishi $

CFLAGS = -c -Wall -O2
OBJECT = HttpClient.o getConnection.o
EXE = HttpClient.exe

$(EXE): $(OBJECT)
	gcc $(OBJECT) -o $(EXE) && strip $(EXE)

HttpClient.o:  HttpClient.c
	gcc $(CFLAGS) HttpClient.c

getConnection.o:  getConnection.c
	gcc $(CFLAGS) getConnection.c

clean:
	rm -f *.o *.exe *~ *.bak

indent:
	indent -br *.c
::::::::::::::
common.h
::::::::::::::
int getConnection(char* hostname);

::::::::::::::
HttpClient.c
::::::::::::::
/*
 * $Id: http2.html,v 1.1 2009/06/22 16:12:13 kishi Exp kishi $ 
 */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
#include <unistd.h>

#include "./common.h"

#define BUF_LEN 8192

int
main (int argc, char *argv[])
{
  int sock;
  char buf[BUF_LEN];
  char *hostname;

  if (argc != 2) {
    printf ("Usage: %s [hostname]\n", argv[0]);
    exit (1);
  }
  hostname = argv[1];

  sock = getConnection (hostname);

  FILE *fp = fdopen (sock, "r+");
  if (fp == NULL) {
    fprintf (stderr, "fdopen failed ...\n");
    return 1;
  }

  // Bufferingしない
  setvbuf (fp, NULL, _IONBF, 0);

  // HTTPリクエスト
  fprintf (fp, "GET / HTTP/1.1\r\n");
  fprintf (fp, "Host: %s\r\n", hostname);
  fprintf (fp, "\r\n");


  //////////////////////////////////////////////////
  // HTTPレスポンスヘッダを取得
  //////////////////////////////////////////////////
  while (1) {
    if (fgets (buf, sizeof (buf), fp) == NULL) {
      break;
    }
    // レスポンスヘッダをリストに格納する
    // --- TO BE IMPLEMENTED --

    // CRLFが来たらヘッダ終了
    if (buf[0] == 0x0d && buf[1] == 0x0a) {
      break;
    }
  }

  //////////////////////////////////////////////////
  // HTTPレスポンスボディを取得
  //////////////////////////////////////////////////
  memset (buf, 0, sizeof (buf));
  while (1) {
    int n;
    n = fread (buf, BUF_LEN, 1, fp);
    if (n > 0) {
      printf ("%s", buf);
      memset (buf, 0, sizeof (buf));
    }
    else {
      break;
    }
  }
  printf ("%s", buf);

  fclose (fp);
  close (sock);

  return 0;

}
::::::::::::::
getConnection.c
::::::::::::::
/*
 * $Id: http2.html,v 1.1 2009/06/22 16:12:13 kishi Exp kishi $
 */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
#include <unistd.h>

#define PORT 80

int
getConnection (char *hostname)
{

  struct sockaddr_in addr;
  struct hostent *he;
  int sock;

  if ((sock = socket (AF_INET, SOCK_STREAM, 0)) < 0) {
    perror ("socket");
    exit (1);
  }

  bzero ((char *) &addr, sizeof (addr));

  if ((he = gethostbyname (hostname)) == NULL) {
    perror ("No such host");
    exit (1);
  }
  bcopy (he->h_addr, &addr.sin_addr, he->h_length);
  addr.sin_family = AF_INET;
  addr.sin_port = htons (PORT);

  if (connect (sock, (struct sockaddr *) &addr, sizeof (addr)) < 0) {
    perror ("connect");
    exit (2);
  }

  return sock;
}
戻る inserted by FC2 system