/* http-netfs.c a trial for http filesystem */

#define _GNU_SOURCE 1
#include <stdio.h>
#include <errno.h>
#include <string.h>
#include <error.h>
#include <netdb.h>
#include <fcntl.h>
#include <argp.h>  
#include <argz.h>
#include <sys/stat.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <hurd/hurd_types.h>
#include <hurd/netfs.h>     

#define MAXBUF 4096

/* Argument Parsing --Variables */
const char *argp_program_version="Http Translator Version 0.1";
static const char args_doc[]="URL";
const char *argp_program_bug_address="arunsark@yahoo.com";
static char doc[]="A trivial Http Translator";
static char *cmd=0;
static size_t cmd_len;
static char **cmdp=0;

volatile struct mapped_time_value *httpfs_maptime;

/* File system data structure */
struct httpfs {
	struct node *root;
	mode_t umask;
	uid_t uid;
	gid_t gid;
	ino_t next_inode;
};

/* Protocol Specific Stuff */	
struct netnode {
	/* URL must have http:// to fetch the file from web server */
	char *url;
	/* Connection Request must not have http:// */
	char *conn_req;
};


struct httpfs *httpfs;


/* Linked list for storing the remote html file received through the socket */
struct http {
	char contents[MAXBUF];
	struct http *next;
};

struct http *html,*tmp,*head;

static struct argp_option options[] = {
{0}
};

/* Argument parsing function */
static error_t parse_opt (int opt,char *arg,struct argp_state *state)
{
	switch (opt) {
	default:
	     return ARGP_ERR_UNKNOWN;
	case ARGP_KEY_INIT:
	case ARGP_KEY_SUCCESS: // --help, --version, --usage options
	case ARGP_KEY_ERROR:
		break;
	case ARGP_KEY_NO_ARGS:
		argp_usage(state);
		return EINVAL;
	case ARGP_KEY_ARGS:
		if ( cmd )
			free(cmd);
		if ( cmdp )
			free(cmdp);
		cmd=0;
		cmdp=0;
		/* Extract the URL from the arguments */
		if (argz_create(state->argv+state->next,&cmd,&cmd_len))
			return ENOMEM;
		if (! (cmdp=(char **)malloc(sizeof(char *)*argz_count(cmd,cmd_len))))
			return ENOMEM;
		argz_extract(cmd,cmd_len,cmdp);
		return 0;
	};
	return 0;
}

static struct argp run_argp = { options,parse_opt,args_doc,doc };

static struct argp_child argp_children[] =
	{ {&netfs_std_startup_argp},{0} };

static struct argp parser =
	{ options,parse_opt,args_doc,doc,argp_children };

struct argp *netfs_runtime_argp = &parser;

error_t netfs_append_args (char **argz,size_t *argz_len);

error_t httpfs_parse_args(int argc,char **argv){
	return argp_parse(&parser,argc,argv,0,0,0);
}

struct netnode *httpfs_make_netnode(char *u,char *c)
{
	struct netnode *nn;
	nn = (struct netnode *)malloc(sizeof(struct netnode));
	nn->url = strdup(u);
	nn->conn_req = strdup(c);
	if (!(nn->url && nn->conn_req) )
	{
		free(nn->url);
		free(nn->conn_req);
		free(nn);
		return NULL;
	}
	return nn;
}

void free_netnode(struct netnode *node)
{
	free(node->url);
	free(node->conn_req);
}

struct node *httpfs_make_node(char *u,char *c)
{
	struct netnode *nn;
	struct node *nd;
	nn = httpfs_make_netnode(u,c);
	if ( !nn)
		return NULL;
	nd = netfs_make_node(nn);
	if (!nd) {
		free(nn);
		return NULL;
	}
	nd->next = NULL;
	nd->prevp = NULL;
	nd->owner = httpfs->uid;
	spin_lock (&netfs_node_refcnt_lock);
	nd->references++;
	spin_unlock(&netfs_node_refcnt_lock);
	nd->nn_stat.st_mode=(S_IRUSR|S_IRGRP|S_IROTH) & ~httpfs->umask;
	nd->nn_stat.st_mode|=S_IFREG;
	nd->nn_stat.st_nlink=1;
	nd->nn_stat.st_uid=httpfs->uid;
	nd->nn_stat.st_gid=httpfs->gid;
	nd->nn_stat.st_rdev=0;
	nd->nn_stat.st_size=0;
	nd->nn_stat.st_blksize=0;
	nd->nn_stat.st_blocks=0;
	nd->nn_stat.st_ino=httpfs->next_inode++;
	fshelp_touch(&nd->nn_stat,TOUCH_ATIME|TOUCH_MTIME|TOUCH_CTIME,httpfs_maptime);
	return nd;
}

void free_node(struct node *np)
{
	free_netnode(np->nn);
	free(np);
}


int main(int argc,char **argv)    
{
        error_t err;
	mach_port_t bootstrap;
	char *url,*conn_req;
	int url_len=0;
	httpfs_parse_args(argc,argv);
	err=maptime_map(0,0,&httpfs_maptime);
	if(err)
		exit(1);
	task_get_bootstrap_port(mach_task_self(),&bootstrap);
	if (bootstrap == MACH_PORT_NULL )
		exit(1);

	/* Get the URL and Connection Request from cmdp */
	url = (char *) malloc(strlen(cmdp[0]) * sizeof(char));	
	conn_req = (char *) malloc( (strlen(cmdp[0])+7) * sizeof(char));

	/* user can give url as http://foo.bar.com or foo.bar.com */
	/* url is just foo.bar.com without http:// */
	if ( !(strcmp((const char*)strndup(cmdp[0],7),"http://")))
		cmdp[0] = cmdp[0] + 7; 
	while ( cmdp[0][url_len] != '/' ) {
		/* Find the URL without http:// */
		url[url_len] = cmdp[0][url_len];
		url_len++;
	}	
        url[url_len]='\0';

	/* Connection Request must start with http:// */
	strcpy(conn_req,"http://");
	strcat(conn_req,cmdp[0]);

	httpfs = (struct httpfs*) malloc(sizeof(struct httpfs));
	httpfs->umask = 0;
	httpfs->uid = 0;
	httpfs->gid = 0;
	httpfs->next_inode = 0;
	httpfs->root = httpfs_make_node(url,conn_req);
	netfs_init();
	netfs_root_node = httpfs->root;
	fprintf(stderr,"Main %s %s",httpfs->root->nn->url,httpfs->root->nn->conn_req);
	netfs_startup(bootstrap,0);
	for ( ; ; ){
		netfs_server_loop();
	}
	free(httpfs);
	return 0;
}

	
error_t open_connection(struct netnode *node,int *sockfd) 
{
	/* Socket and IP addresses */
	struct sockaddr_in dest;
	struct hostent *hptr;
	/* Capture the byte stream from socket */
	char buffer[MAXBUF];
	size_t to_write;
	ssize_t written=0;

	if ( (*sockfd = socket(AF_INET, SOCK_STREAM, 0)) < 0 ){
		abort();
        }

        /* Initialize server address/port struct */
        bzero(&dest, sizeof(dest));
        dest.sin_family = AF_INET;
        dest.sin_port = htons(80); /*default HTTP Server port */
	
	/* Find IP address for foo.bar.com --lookup at /etc/hosts */
	if ( (hptr=gethostbyname(node->url)) == NULL ) {
		abort();
	}

        if ( inet_addr(hptr->h_addr, &dest.sin_addr.s_addr) == 0 ){
		abort();
	}

        /* Connect to server */
        if ( connect(*sockfd, (struct sockaddr*)&dest, sizeof(dest)) != 0 ){
		abort();
	}

        /* Send GET request */ 
	sprintf(buffer, "GET %s HTTP/1.0\n\n", node->conn_req);
	to_write = sizeof(buffer);
	written = (write(*sockfd,buffer,to_write));
	if ( written == -1 || written < to_write )
		abort();
	return 0;
}



/* httpfs interfaces to netfs */
error_t netfs_attempt_read(struct iouser *cred,struct node *node,off_t offset,
			   size_t *len,void *data)
{
	error_t err;
	int remote_fd;
	ssize_t bytes_read;
	ssize_t content_len=0;
	char buffer[MAXBUF];
	char *contents;
	head=NULL;
	err = open_connection(node->nn,&remote_fd);
	if ( !err )
		return err;
	
        /* While there's data, read and store it in linked list */
        do
        {
        	bzero(buffer, sizeof(buffer));
	        bytes_read = read(remote_fd,buffer,sizeof(buffer)-sizeof(char));
       		if ( bytes_read > 0 ) {
		if ( head == NULL ) {
			/* GET returns HEAD + html file */
			html=(struct http*) malloc(sizeof(struct http));
			strcpy(html->contents,buffer);
			html->next = NULL;
			head = html;
			content_len = content_len + strlen(html->contents);
		}
		else {
			/* Store the byte stream in linked list */
			tmp=(struct http*) malloc(sizeof(struct http));
			strcpy(tmp->contents,buffer);
			tmp->next = NULL;
			html->next = tmp;
			html = tmp;
			content_len = content_len + strlen(html->contents);
		}
		}
    	} while ( bytes_read > 0 );

	if ( content_len > 0 ) {
		/* Allocate the single unified buffer */
		contents = (char *) malloc(content_len);
		bzero(contents,sizeof(contents));
		for(tmp=head;tmp!=NULL;tmp=tmp->next)
			strcat(contents,tmp->contents);

		/* No need for contents, can copy directly to data */
		data = (void *) malloc(content_len);
		memcpy((char *)data,contents,content_len);
		*len = (size_t) content_len;
	}
	else
		err = errno;
	return err;
}

/* Attempt to create a file named NAME in DIR for USER with MODE.  Set *NODE
   to the new node upon return.  On any error, clear *NODE.  *NODE should be
   locked on success; no matter what, unlock DIR before returning.  */
error_t 
netfs_attempt_create_file (struct iouser *user, struct node *dir,
	       	           char *name, mode_t mode, struct node **node)
{
	*node = NULL;
	mutex_unlock (&dir->lock);
	return EROFS;
}

/* Node NODE is being opened by USER, with FLAGS.  NEWNODE is nonzero if we
   just created this node.  Return an error if we should not permit the open
   to complete because of a permission restriction. */
error_t
netfs_check_open_permissions (struct iouser *user, struct node *node,
			      int flags, int newnode)
{
	error_t err = 0;
	fprintf(stderr,"Inside check open permissions\n");
	if (!err && (flags & O_READ))
		err = fshelp_access (&node->nn_stat, S_IREAD, user);
	if (!err && (flags & O_WRITE))
		err = fshelp_access (&node->nn_stat, S_IWRITE, user);
	if (!err && (flags & O_EXEC))
		err = fshelp_access (&node->nn_stat, S_IEXEC, user);
	return err;
}

/* This should attempt a utimes call for the user specified by CRED on node
   NODE, to change the atime to ATIME and the mtime to MTIME. */
error_t
netfs_attempt_utimes (struct iouser *cred, struct node *node,
		      struct timespec *atime, struct timespec *mtime)
{
	error_t err = 0;
	int flags = TOUCH_CTIME;
	fprintf(stderr,"Inside attemp utimes\n");

	if (!err)
		err = fshelp_isowner (&node->nn_stat, cred);

	if (!err) {
		if (atime) {
			node->nn_stat.st_atime = atime->tv_sec;
			node->nn_stat.st_atime_usec = atime->tv_nsec / 1000;
		} else
			flags |= TOUCH_ATIME;

		if (mtime) {
			node->nn_stat.st_mtime = mtime->tv_sec;
			node->nn_stat.st_mtime_usec = mtime->tv_nsec / 1000;
		} else
			flags |= TOUCH_MTIME;

		fshelp_touch (&node->nn_stat, flags, httpfs_maptime);
	}

	return err;
}

/* Return the valid access types (bitwise OR of O_READ, O_WRITE, and O_EXEC)
   in *TYPES for file NODE and user CRED.  */
error_t
netfs_report_access (struct iouser *cred, struct node *node, int *types)
{
	*types = 0;
	fprintf(stderr,"Report access\n");
	if (fshelp_access (&node->nn_stat, S_IREAD, cred) == 0)
		*types |= O_READ;
	if (fshelp_access (&node->nn_stat, S_IWRITE, cred) == 0)
		*types |= O_WRITE;
	if (fshelp_access (&node->nn_stat, S_IEXEC, cred) == 0)
		*types |= O_EXEC;

	return 0;
}

/* Trivial definitions.  */

/* Make sure that NP->nn_stat is filled with current information.  CRED
   identifies the user responsible for the operation.  */
error_t netfs_validate_stat (struct node *node, struct iouser *cred)
{
	return 0;
}

/* This should sync the file NODE completely to disk, for the user CRED.  If
   WAIT is set, return only after sync is completely finished.  */
error_t
netfs_attempt_sync (struct iouser *cred, struct node *node, int wait)
{
	return 0;
}

/* Fetch a directory  */
error_t
netfs_get_dirents (struct iouser *cred, struct node *dir,
                   int first_entry, int max_entries, char **data,
	           mach_msg_type_number_t *data_len,
                   vm_size_t max_data_len, int *data_entries)
{
		fprintf(stderr,"Inside get dirents\n");
		return ENOTDIR;
}

/* Lookup NAME in DIR for USER; set *NODE to the found name upon return.  If
   the name was not found, then return ENOENT.  On any error, clear *NODE.
   (*NODE, if found, should be locked, this call should unlock DIR no matter
   what.) */
error_t netfs_attempt_lookup (struct iouser *user, struct node *dir,
			      char *name, struct node ** node)
{
	fprintf(stderr,"Inside attempt lookup\n");
	return ENOTDIR;
}

/* Delete NAME in DIR for USER. */
error_t netfs_attempt_unlink (struct iouser *user, struct node *dir,
			      char *name)
{
	return EROFS;
}

/* Note that in this one call, neither of the specific nodes are locked. */
error_t netfs_attempt_rename (struct iouser *user, struct node *fromdir,
		              char *fromname, struct node *todir,
		              char *toname, int excl)
{
	return EROFS;
}

/* Attempt to create a new directory named NAME in DIR for USER with mode
   MODE.  */
error_t netfs_attempt_mkdir (struct iouser *user, struct node *dir,
			     char *name, mode_t mode)
{
	return EROFS;
}

/* Attempt to remove directory named NAME in DIR for USER. */
error_t netfs_attempt_rmdir (struct iouser *user,
			     struct node *dir, char *name)
{
	return EROFS;
}

/* This should attempt a chmod call for the user specified by CRED on node
   NODE, to change the owner to UID and the group to GID. */
error_t netfs_attempt_chown (struct iouser *cred, struct node *node,
			     uid_t uid, uid_t gid)
{
	return EROFS;
}

/* This should attempt a chauthor call for the user specified by CRED on node
   NODE, to change the author to AUTHOR. */
error_t netfs_attempt_chauthor (struct iouser *cred, struct node *node,
				uid_t author)
{
	return EROFS;
}

/* This should attempt a chmod call for the user specified by CRED on node
   NODE, to change the mode to MODE.  Unlike the normal Unix and Hurd meaning
   of chmod, this function is also used to attempt to change files into other
   types.  If such a transition is attempted which is impossible, then return
   EOPNOTSUPP.  */
error_t netfs_attempt_chmod (struct iouser *cred, struct node *node,
			     mode_t mode)
{
	return EROFS;
}

/* Attempt to turn NODE (user CRED) into a symlink with target NAME. */
error_t netfs_attempt_mksymlink (struct iouser *cred, struct node *node,
				 char *name)
{
	return EROFS;
}

/* Attempt to turn NODE (user CRED) into a device.  TYPE is either S_IFBLK or
   S_IFCHR. */
error_t netfs_attempt_mkdev (struct iouser *cred, struct node *node,
			     mode_t type, dev_t indexes)
{
	return EROFS;
}

/* Attempt to set the passive translator record for FILE to ARGZ (of length
   ARGZLEN) for user CRED. */
error_t netfs_set_translator (struct iouser *cred, struct node *node,
			      char *argz, size_t argzlen)
{
	return EROFS;
}

#if 0
/* The user may define this function (but should define it together with
   netfs_set_translator).  For locked node NODE with S_IPTRANS set in its
   mode, look up the name of its translator.  Store the name into newly
   malloced storage, and return it in *ARGZ; set *ARGZ_LEN to the total
   length.  */
error_t netfs_get_translator (struct node *node, char **argz, size_t *argz_len)
{
}
#endif

/* This should attempt a chflags call for the user specified by CRED on node
   NODE, to change the flags to FLAGS. */
error_t netfs_attempt_chflags (struct iouser *cred, struct node *node,
			       int flags)
{
	return EROFS;
}

/* This should attempt to set the size of the file NODE (for user CRED) to
   SIZE bytes long. */
error_t netfs_attempt_set_size (struct iouser *cred, struct node *node,
				off_t size)
{
	return EROFS;
}

/* This should attempt to fetch filesystem status information for the remote
   filesystem, for the user CRED. */
error_t netfs_attempt_statfs (struct iouser *cred, struct node *node,
			      struct statfs *st)
{
	return EOPNOTSUPP;
}

/* This should sync the entire remote filesystem.  If WAIT is set, return
   only after sync is completely finished.  */
error_t netfs_attempt_syncfs (struct iouser *cred, int wait)
{
	return 0;
}

/* Create a link in DIR with name NAME to FILE for USER.  Note that neither
   DIR nor FILE are locked.  If EXCL is set, do not delete the target, but
   return EEXIST if NAME is already found in DIR.  */
error_t netfs_attempt_link (struct iouser *user, struct node *dir,
			    struct node *file, char *name, int excl)
{
	return EROFS;
}

/* Attempt to create an anonymous file related to DIR for USER with MODE.
   Set *NODE to the returned file upon success.  No matter what, unlock DIR. */
error_t netfs_attempt_mkfile (struct iouser *user, struct node *dir,
			      mode_t mode, struct node ** node)
{
	*node = NULL;
	mutex_unlock (&dir->lock);
	return EROFS;
}

/* maximum numer of symlinks, does not really apply, so set to 0 */
int netfs_maxsymlinks = 0;
/* Read the contents of NODE (a symlink), for USER, into BUF. */
error_t netfs_attempt_readlink (struct iouser *user, struct node *node,
				char *buf)
{
	return EINVAL;
}

/* Write to the file NODE for user CRED starting at OFSET and continuing for up
   to *LEN bytes from DATA.  Set *LEN to the amount seccessfully written upon
   return. */
error_t netfs_attempt_write (struct iouser *cred, struct node *node,
			     off_t offset, size_t *len, void *data)
{
	return EROFS;
}

/* XXX doesn't say anywhere what this must do */
/* The user must define this function.  Create a new user
   from the specified UID and GID arrays. */

/* The user must define this function.  Node NP is all done; free
   all its associated storage. */
void netfs_node_norefs (struct node *np)
{
	mutex_lock (&np->lock);
	*np->prevp = np->next;
	np->next->prevp = np->prevp;
	free_node (np);
	/* XXX: remove node from tree and delete the cache entry */
}

