Welcome, guest! Login / Register - Why register?
Psst.. new poll here.
Psst.. new forums here.
Microsoft is blocking us again (TY IP Reputation!) so just use oauth login instead. :)

Paste

Pasted as C by Petr ( 14 years ago )
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <string.h>
#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
#include <netdb.h>

void tiskni_hlavicku(char* msg, const char* nazev_hlavicky)
{
    char* tisk;
    char tisk_buffer[1000];
    int k = 0;
   tisk = strstr(msg, nazev_hlavicky);
   while (tisk[k] != '\n')
   {
     if (tisk[k] == '\r')
     {
       strncpy( tisk_buffer, tisk, strspn(tisk, &tisk;[k - 1]));
     }
     k++;
   }
   printf ("%s\n",&tisk;_buffer);
}

int main (int argc, char *argv[] )
{
  unsigned int i = 0, k =0;

  char* url = argv[argc-1]; // parser
  if (strstr(url, "://") != NULL)
  {
    url = strstr(url, "://") + strlen("://"); // http
  }
  char* parsedhost = url;  //adresa hosta
  char* parsedpath = "";  //cesta
  char* parsedport = "80";  // port
  /*projeti cele adresy*/
  while (url[i] != '\0')
  {

    if (url[i] == ':') //ziskani portu
    {
      url[i] = '\0';
      parsedport = url + i + 1;
    }

    if (url[i] == '/') // get path
    {
      url[i] = '\0';
      parsedpath = url + i + 1;
      break;

    }


    i++;

  }
  if (strlen(parsedpath) < 1) // odstraneni koncoveho /
    {
      parsedpath = "";
    }

  int mySocket, n;
  struct sockaddr_in sin; struct hostent *host;
  char msg[5000];

  msg[0] = '\0';
  strncat(msg, "HEAD /",40);
  strncat(msg, parsedpath,40);
  strncat(msg, " HTTP/1.1\r\nHost: ",40);
  strncat(msg, parsedhost,40);
  strncat(msg, "\r\n\r\n",40);
  if ( (mySocket = socket(AF_INET, SOCK_STREAM, 0 ) ) == -1 ) { /* create socket*/
    perror("error on socket");  /* socket error */
    return -1;
  }
  sin.sin_family = AF_INET;              /*set protocol family to Internet */
  sin.sin_port = htons(80);  /* set port no. */
  host =  gethostbyname(parsedhost);
  if ( host == NULL){
    fprintf(stderr, "gethostname error: %s", parsedhost);
    perror("gethostname error");
    return -1;
   }
  memcpy( &sin;.sin_addr, host->h_addr, host->h_length);
  if (connect (mySocket, (struct sockaddr *)&sin;, sizeof(sin) ) == -1 ){
    perror("error on connect"); return -1;   /* connect error */
  }

  if ( write(mySocket, msg, strlen(msg)) == -1 ) {  /* send message to server */
    perror("error on write");    return -1; /*  write error */
  }
  if ( ( n = read(mySocket, msg, sizeof(msg) ) ) == -1) {  /* read message from server */
    perror("error on read"); return -1; /*  read error */
  }


  unsigned int response = atoi(msg +9);
  char* location = NULL;

  if (response >= 400)
  {
    fprintf(stderr, "%d: Naprav to\n", response);
    return -1;
  }

  if (response >=300 && response < 400)
  {
    fprintf(stderr, "Moc presmeroveni!\n");
    return -1;
  }

  /** tisk **/

  if (argc == 2)
    {
      printf (" %s\n ",msg);
    }
  else
  {
    for(i = 1; i < (argc-1);i++)
      {
 if (strcmp("-l", argv[i]) == 0)  // delka
 {
   tiskni_hlavicku(msg, "Content-Length");
 }
 if (strcmp("-m", argv[i]) == 0)  // aktualizace
 {
     tiskni_hlavicku(msg, "Last-Modified");
 }
 if (strcmp("-t", argv[i]) == 0)  //typ
 {
   tiskni_hlavicku(msg, "Content-Type");
 }
 if (strcmp("-s", argv[i]) == 0)  //server
 {
        tiskni_hlavicku(msg, "Server");
 }


      }
  }

  if (close(mySocket) < 0) {
    perror("error on close");   /* close error */
    return -1;
  }
  return 0;
}

 

Revise this Paste

Parent: 46969
Children: 46974
Your Name: Code Language: