Php miniserver downloader bandwidth

Posted by snikolov on Stack Overflow See other posts from Stack Overflow or by snikolov
Published on 2010-05-31T09:47:01Z Indexed on 2010/05/31 9:53 UTC
Read the original article Hit count: 307

Filed under:
|
|
  $httpsock = @socket_create_listen("9090");
  if (!$httpsock) {
    print "Socket creation failed!\n";
    exit;
  }
  while (1) {
    $client = socket_accept($httpsock);
    $input = trim(socket_read ($client, 4096));
    $input = explode(" ", $input);
    $input = $input[1];
    $fileinfo = pathinfo($input);

    switch ($fileinfo['extension']) {
      default:
        $mime = "text/html";
    }
    if ($input == "/") {
      $input = "index.html";
    }
    $input = ".$input";
    if (file_exists($input) && is_readable($input)) {
      echo "Serving $input\n";
      $contents = file_get_contents($input);
      $output = "HTTP/1.0 200 OK\r\nServer: APatchyServer\r\nConnection: close\r\nContent-Type: $mime\r\n\r\n$contents";
    } else {
      //$contents = "The file you requested doesn't exist.  Sorry!";
      //$output = "HTTP/1.0 404 OBJECT NOT FOUND\r\nServer: BabyHTTP\r\nConnection: close\r\nContent-Type: text/html\r\n\r\n$contents";

      $filename = "dada";
      $file     = fopen($filename, 'r');
      $filesize = filesize($filename);
      $buffer   = fread($file, $filesize);
      $send    = array("Output"=>$buffer,"filesize"=>$filesize,"filename"=>$filename);

      $file   = $send['filename'];
      $filesize  = $send['filesize'];
      $output  = 'HTTP/1.0 200 OK\r\n';
      $output .= "Content-type: application/octet-stream\r\n";
      $output .= 'Content-Disposition: attachment; filename="'.$file.'"\r\n';
      $output .= "Content-Length:$filesize\r\n";
      $output .= "Accept-Ranges: bytes\r\n";
      $output .= "Cache-Control: private\n\n";
      $output .= $send['Output'];
      $output .= "Content-Transfer-Encoding: binary";
      $output .= "Connection: Keep-Alive\r\n";

    }
    socket_write($client, $output);
    socket_close ($client);
  }
  socket_close ($httpsock);

Hello i have create a code here to work as a webserver downloader the client can request files and download, i got it working at last however, i would like to know if the webserver can show much bandwidth the user request via sockets, perl has the same option as php but its more hardcore than php, i dont understand much about perl, i even saw that a miniwebserver can show much the client user pulls from the server would it be possible that you can assist me with this coding, i much aprreciate it thank you guys.

© Stack Overflow or respective owner

Related posts about php

Related posts about sockets