file access (open and pipe?) problem - Programmers Heaven

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

file access (open and pipe?) problem

eatfishcrackereatfishcracker Posts: 7Member
I'm writing a simple web server...
I want to be able to send an HTML file to the client when they connect.
I have no idea how to do this- right now I'm using the code below to send an HTML "message" to the client. This displays ok in the browser- but I cannot link to other pages...
How do make it so that i can open an individual file off of the server (and send it to the client) instead of just a string coded into the source of the webserver?

#code excerpt below...
$msg = "


Web Server Page


This page is generated by the server! I cannot figure out how to have
it link to a file!

";
#send the message
$new_sock->send($msg);

Comments

  • SouldrinkerSouldrinker Posts: 143Member
    ehm... writing a webserver in perl is always a favorite ;) I won`t describe the webserver here - only the file reading:

    my $FileContent = "";

    open ( FILEHANDLE, "<[Location of your File" );
    while ( <FILEHANDLE> )
    {
    $FileContent .= $_;
    }
    close ( FILEHANDLE );

    Now in "$FileContent" there is all of the content in the specified file.
    best regards,

    sebastian mohrenstecher
    executive secretary
    net::allies

    www.net-allies.de
    [email protected]

  • JonathanJonathan Posts: 2,914Member
    : ehm... writing a webserver in perl is always a favorite ;) I won`t
    : describe the webserver here - only the file reading:
    :
    : my $FileContent = "";
    :
    : open ( FILEHANDLE, "<[Location of your File" );
    : while ( <FILEHANDLE> )
    : {
    : $FileContent .= $_;
    : }
    : close ( FILEHANDLE );
    :
    : Now in "$FileContent" there is all of the content in the specified
    : file.
    I always liked:-
    open ( FILEHANDLE, "<[Location of your File" );
    my $FileContent = join('', <FILEHANDLE>);
    close ( FILEHANDLE );
    But that's just me being choosy. Better you can undef $/ and slurp the whole file. So yay, we have three ways of reading a file now.

    On the web server stuff, you need to look at the headers that are recieved by the server. Find one that looks like:-

    GET /path/to/file.htm

    You can from there extract the path to load. [b]Be very careful when you start using this in your open statement.[/b] Otherwise, you will give people access to virtually everything on the server that the user the server is running as has access to. Think hard about sequences of ../'s and the like, and how you will handle them and make sure the user can't access stuff they can't. Feel free to post that bit of your code here for anyone who stumbles by to glance over it.

    Hope this helps,

    Jonathan

    ###
    for(74,117,115,116){$::a.=chr};(($_.='qwertyui')&&
    (tr/yuiqwert/her anot/))for($::b);for($::c){$_.=$^X;
    /(p.{2}l)/;$_=$1}$::b=~/(..)$/;print("$::a$::b $::c hack$1.");

  • eatfishcrackereatfishcracker Posts: 7Member
    I got the server more functional than before...
    I can only read the last line of a file- therefore causing me to put all of my HTML code on one line...
    im newbie to perl so i don't really know how to fix that.
    It works though- it opens the file and reads it to the browser- the one problem i would have is that I cannot have links to other pages on my server. I have no idea how i would be able to do that- and as the code is right now- its not possible because I am piping data from one file and that file isnt exactly a file manager...
    code for the server below...

    use IO::Socket::INET;
    #create socket
    my $sock = new IO::Socket::INET->new(
    LocalPort => '80',
    Proto => 'tcp',
    Listen => 1,
    Reuse => 1,
    );
    #error handling
    die "Could not create socket: $!
    " unless $sock;
    #tell user that server is running
    print "Server running on port 80";
    #loop the accept clients- so that it doesn't close after serving once
    for ($count=1; $count<11; $count++){
    #accept clients
    my $new_sock = $sock->accept();
    #if a client comes, send the following info
    if(defined(<$new_sock>)) {
    #mandatory statement
    print $_;
    #file piping below. HTML files created must be 1 line long- you can just have all of
    #your HTML code on one line. it'll still work but its still a problem

    open ( FH, "C:\windows\desktop\index.html");
    while( )
    {
    $FileContent = $_;
    }
    close ( FH );

    #send the message
    $new_sock->send($FileContent);
    #close up shop
    }
    }


  • JonathanJonathan Posts: 2,914Member
    : I got the server more functional than before...
    : I can only read the last line of a file- therefore causing me to put
    : all of my HTML code on one line...
    That's because:-

    : while( )
    : {
    : $FileContent = $_;
    : }
    You should be using .= instead of =. In the while loop each line of the file is put into $_ each time you loop. You are over-writing $FileContent with the contents of the line just read in. You need to instead concatenate it onto what you've already read, which is what .= does.

    [code]while () {
    $FileContent .= $_;
    }[/code]

    : im newbie to perl so i don't really know how to fix that.
    No worries, that's what this board is for. :-) Another way to do it, FYI, is:-
    $FileContent = join('', );
    Which puts into array context, and it returns an array of all lines which we then join. Not sure it's the most efficient way, but it's a common idiom. The other way is:-

    [code]{
    local $/ = undef;
    $FileContent = :
    }[/code]
    Here we undefine $/, which stops the file just being read line by line and reads the lot ($/ is the input seperator, and defaults to
    ). Note how we put that in a closure and use local, so it only affects that particular read.

    : It works though- it opens the file and reads it to the browser- the
    : one problem i would have is that I cannot have links to other pages
    : on my server. I have no idea how i would be able to do that- and as
    : the code is right now- its not possible because I am piping data
    : from one file and that file isnt exactly a file manager...


    : #loop the accept clients- so that it doesn't close after serving once
    : for ($count=1; $count<11; $count++){
    : #accept clients
    : my $new_sock = $sock->accept();
    : #if a client comes, send the following info
    : if(defined(<$new_sock>)) {
    : #mandatory statement
    : print $_;
    What does $_ contain here? Note that to know what file to serve, you need to read data that was sent by the client.

    : #send the message
    : $new_sock->send($FileContent);
    Would you not have to sent a Content-type header before doing this?

    Jonathan

    ###
    for(74,117,115,116){$::a.=chr};(($_.='qwertyui')&&
    (tr/yuiqwert/her anot/))for($::b);for($::c){$_.=$^X;
    /(p.{2}l)/;$_=$1}$::b=~/(..)$/;print("$::a$::b $::c hack$1.");

Sign In or Register to comment.