Solved

Retrieving remote file information

Posted on 1997-07-07
2
147 Views
Last Modified: 2013-12-25
Is it possible to use PERL and/or CGI to query a remote server to retrieve file information?  Basically, I am looking to get the date created or last modified date of files on different servers.  If it is possible to do this, how is it done (please provide code if possible).  What information can you retrieve about remote files on servers you access anonymously?  Thanks for your assistance.  
0
Comment
Question by:mtob
2 Comments
 
LVL 5

Accepted Solution

by:
julio011597 earned 100 total points
ID: 1828904
Sure, i'll give a ready routine to get file informations through use of the web server's HEAD method.

You can embedd this routine anywhere you need; just pass it the right parameters - this means you could put it into a program to invoke from the shell, or into a CGI.

Please check function return types and include files on your OS, since there may be slight differences.

--//--
#include <stdlib.h>
#include <stdio.h>
#include <string.h>     /* bzero() and bcopy() */
#include <netdb.h>      /* gethostbyname() */
#include <sys/socket.h> /* socket() and connect() */
#include <arpa/inet.h>  /* htons() */
#include <errno.h>      /* errno */

#define PORT 80

/* get_url()
 * answer: file to write to (must be already open for writing - it may be stdin)
 * host: e.g. "www.experts-exchange.com"
 * url : e.g. "/topics/comp/lang/c"
 * err_str: to eventually store an error message
 *
 * returns 0 if succesful, errno otherwise
 */

int get_url(FILE *answer, const char *host, const char *url, char err_str[])
{
  int sock;
  struct hostent *hep;
  struct sockaddr_in sai;
  FILE *infile, *outfile;
  char buff[BUFSIZ];

  if(!(hep = gethostbyname(host))) {
    strcpy(err_str, "gethostbyname error");
    return errno;
  }
  if((sock = socket(AF_INET, SOCK_STREAM, 0)) == -1) {
    strcpy(err_str, "socket error");
    return errno;
  }

  bzero((char *)&sai, sizeof(struct sockaddr_in));
  sai.sin_family = AF_INET;
  sai.sin_port = htons((unsigned short)PORT);
  bcopy(hep->h_addr, (char *)&(sai.sin_addr.s_addr), hep->h_length); /* note: could just be 'sai.sin_addr' */

  if(connect(sock, (struct sockaddr *)&sai, sizeof(sai)) == -1) {
    strcpy(err_str, "connect error");
    return errno;
  }

  /* fine: now we are connected */
  /* let's work with stdio */
  if(!(infile = fdopen(sock, "r"))) {
    strcpy(err_str, "fdopen(in) error");
    return errno;
  }
  if(!(outfile = fdopen(sock, "w"))) {
    strcpy(err_str, "fdopen(out) error");
    return errno;
  }

  /* now, the query: i'll leave error checking now on! */
  fprintf(outfile, "HEAD %s HTTP1.0\n", url);
  fflush(outfile); /* this is mandatory: outfile would buffer */

  while(fgets(buff, BUFSIZ, infile)) fputs(buff, answer);
  fflush(answer); /* this is not mandatory:) */

  /* cleanup */
  /* note: the two fclose's are also mandatory: */
  /* the file tables are not automagically deallocated */
  /* if you just close the socket (at least on my system), */
  /* and you could go out of resources if you loop */
  /* to connect to more than one site */
  fclose(infile); fclose(outfile);
  close(sock);

  return 0;
}
--//--

Cheers, julio

P.S. maybe this is worth more than 100 points...
0
 

Author Comment

by:mtob
ID: 1828905
You're right, it was worth more.  Thank you very much.  If you could help though, is there a PERL way of doing it.  I can do it through C if I have to but would much rather through PERL.
0

Featured Post

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Making a simple AJAX shopping cart Couple years ago I made my first shopping cart, I used iframe and JavaScript, it was very good at that time, there were no sessions or AJAX, I used cookies on clients machine. Today we have more advanced techno…
It is a general practice to get rid of old user profiles on a computer  in a LAN environment. As I have been working with a company in a LAN environment where users move from one place to some other place at times. This will make many user profil…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…

839 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question