Re: [Vala] http server help



I'm an idiot!
The first code, with socket, I forget the new line to close the header, but
with the server libsoup I have forgotten the 200 OK response.


2011/10/1 andrea zambon <zamby ing gmail com>

Done.

void default_handler (Soup.Server server, Soup.Message msg, string path,
    GLib.HashTable? query, Soup.ClientContext client)
{

    try {

        var file = File.new_for_path ("audio.mp3");
        var file_stream = file.read ();
        var data_stream = new DataInputStream (file_stream);
            data_stream.set_byte_order (DataStreamByteOrder.LITTLE_ENDIAN);
            uint8[] buffer = new uint8[5000000];
            file_stream.seek(0, SeekType.CUR);
            data_stream.read (buffer);
          msg.set_response ("audio/mpeg", Soup.MemoryUse.COPY, buffer);

        } catch (GLib.Error e) {

        stderr.printf ("%s\n", e.message);
        } catch (GLib.IOError e) {

            stderr.printf ("%s\n", e.message);
    }
}

int main () {
    try {
            var server = new Soup.Server (Soup.SERVER_PORT, 8088);
            server.add_handler ("/test", default_handler);
            server.run ();

        } catch (Error e) {
            stderr.printf ("%s\n", e.message);
            return 1;
        }
        return 0;
}

Bye.




2011/10/1 andrea zambon <zamby ing gmail com>

Ok. I understand. thanks very much.


2011/10/1 Luca Bruno <lethalman88 gmail com>

On Sat, Oct 1, 2011 at 4:57 PM, andrea zambon <zamby ing gmail com>wrote:

Thanks for the reply.

I tried with http 1.1 does not work.
This is only a test, I will not specify the length of the data because they
do not always know.

<?php
$file = 'audio.mp3';
if (file_exists($file)) {
    header('Content-Type: audio/mpeg');
    ob_clean();
    flush();
    readfile($file);
    exit;
}
?>


This example works in php. I would write it in vala.


Php and apache (or any other server) do really much more under the hood.
If you want the same in vala, you want at least to study the HTTP specs or
use some helper library like libsoup or anything else. Also, I didn't say
using HTTP 1.1 would have fixed the issue.
In general you can know the file size, it's not a stream. If you don't
know, then you want chunked encoding. This isn't something vala will do for
you, you have to implement the http rfc.

--
www.debian.org - The Universal Operating System






[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]