adamdruppe / arsd

This is a collection of modules that I've released over the years. Most of them stand alone, or have just one or two dependencies in here, so you don't have to download this whole repo.
http://arsd-official.dpldocs.info/arsd.html
531 stars 128 forks source link

cgi.d (WebSocket server): Invalid data on 2nd recv & huge execution time #247

Closed andre2007 closed 4 years ago

andre2007 commented 4 years ago

On 2nd recv invalid data is received. Please see this echo example. recv_buffer_1 is different than recv_buffer_2.

Also another question. The execution time of this sample is rather long, several seconds. This seems also a bug.

Python

import asyncio
import websockets

async def hello():
    data = b'\x00\x00\x00G{"opconflen": 0, "funcconflen": 0, "dflens": [4078], "transformed": []}PAR1\x15\x04\x15`\x15dL\x15\x0c\x15\x04\x12\x00\x000\xbc\xefG7G\t\xc7\x01\xc0Cy9\xd2G\x1b\xf4\xbf\x8b\xfb\x1a<\x14=\xe3\xbf\xc66\xfc\xa1\xd4\x00\xe9\xbf-<\x19m\x87\xa4\xc6?l\x06_\xe7,\xfc\xcf?\x15\x00\x15\x16\x15\x1a,\x15\x0c\x15\x04\x15\x06\x15\x06\x1c\x18\x08l\x06_\xe7,\xfc\xcf?\x18\x08\xefG7G\t\xc7\x01\xc0\x16\x00(\x08l\x06_\xe7,\xfc\xcf?\x18\x08\xefG7G\t\xc7\x01\xc0\x00\x00\x00\x0b(\x02\x00\x00\x00\x0c\x01\x03\x03\x88\xc6\x02&\x9c\x02\x1c\x15\n\x195\x04\x00\x06\x19\x18\x01A\x15\x02\x16\x0c\x16\x8c\x02\x16\x94\x02&\x88\x01&\x08\x1c\x18\x08l\x06_\xe7,\xfc\xcf?\x18\x08\xefG7G\t\xc7\x01\xc0\x16\x00(\x08l\x06_\xe7,\xfc\xcf?\x18\x08\xefG7G\t\xc7\x01\xc0\x00\x00\x00\x15\x04\x15`\x15dL\x15\x0c\x15\x04\x12\x00\x000\xbcv\x00\x87\xc6\\G\xeb?r\xd8\xdf\x96\xc9\xae\xdc?\x97;\xb8\x99b\xfa\xdd\xbf;_\xaeV\xe4\xa6\xdf\xbf\x08\x07\x7f{\xb5\xf6\x9a\xbf\xd6\xa2\x87\x03\xcc\xc6\xa5\xbf\x15\x00\x15\x16\x15\x1a,\x15\x0c\x15\x04\x15\x06\x15\x06\x1c\x18\x08v\x00\x87\xc6\\G\xeb?\x18\x08;_\xaeV\xe4\xa6\xdf\xbf\x16\x00(\x08v\x00\x87\xc6\\G\xeb?\x18\x08;_\xaeV\xe4\xa6\xdf\xbf\x00\x00\x00\x0b(\x02\x00\x00\x00\x0c\x01\x03\x03\x88\xc6\x02&\xc8\x05\x1c\x15\n\x195\x04\x00\x06\x19\x18\x01B\x15\x02\x16\x0c\x16\x8c\x02\x16\x94\x02&\xb4\x04&\xb4\x03\x1c\x18\x08v\x00\x87\xc6\\G\xeb?\x18\x08;_\xaeV\xe4\xa6\xdf\xbf\x16\x00(\x08v\x00\x87\xc6\\G\xeb?\x18\x08;_\xaeV\xe4\xa6\xdf\xbf\x00\x00\x00\x15\x04\x15`\x15dL\x15\x0c\x15\x04\x12\x00\x000\xbc\xda_s\xc7iD\xcd\xbf\x90~\xdf\x9d\x8c\x0f\xbf\xbf\'&:\xe8s\xd7\xef?E\x127\xd0,\xd5\xd4\xbf\xa2\x95\x0f\n\x0c\x85\xde\xbff(\\\xe8d\xd3\xf8?\x15\x00\x15\x16\x15\x1a,\x15\x0c\x15\x04\x15\x06\x15\x06\x1c\x18\x08f(\\\xe8d\xd3\xf8?\x18\x08\xa2\x95\x0f\n\x0c\x85\xde\xbf\x16\x00(\x08f(\\\xe8d\xd3\xf8?\x18\x08\xa2\x95\x0f\n\x0c\x85\xde\xbf\x00\x00\x00\x0b(\x02\x00\x00\x00\x0c\x01\x03\x03\x88\xc6\x02&\xf6\x08\x1c\x15\n\x195\x04\x00\x06\x19\x18\x01C\x15\x02\x16\x0c\x16\x8c\x02\x16\x94\x02&\xe2\x07&\xe2\x06\x1c\x18\x08f(\\\xe8d\xd3\xf8?\x18\x08\xa2\x95\x0f\n\x0c\x85\xde\xbf\x16\x00(\x08f(\\\xe8d\xd3\xf8?\x18\x08\xa2\x95\x0f\n\x0c\x85\xde\xbf\x00\x00\x00\x15\x04\x15`\x15dL\x15\x0c\x15\x04\x12\x00\x000\xbc\xefq\\e\xb6\x84\xbb?\x0e\xf9\x0f{k\xac\x01\xc0\xa1\x88j\xb8"6\xf2\xbf\x17|\xf0hF\xd2\xf2\xbf#\xef\xa7F\xe4\xa6\xcc\xbf\x93/(\x18\xd6J\xe3?\x15\x00\x15\x16\x15\x1a,\x15\x0c\x15\x04\x15\x06\x15\x06\x1c\x18\x08\x93/(\x18\xd6J\xe3?\x18\x08\x0e\xf9\x0f{k\xac\x01\xc0\x16\x00(\x08\x93/(\x18\xd6J\xe3?\x18\x08\x0e\xf9\x0f{k\xac\x01\xc0\x00\x00\x00\x0b(\x02\x00\x00\x00\x0c\x01\x03\x03\x88\xc6\x02&\xa4\x0c\x1c\x15\n\x195\x04\x00\x06\x19\x18\x01D\x15\x02\x16\x0c\x16\x8c\x02\x16\x94\x02&\x90\x0b&\x90\n\x1c\x18\x08\x93/(\x18\xd6J\xe3?\x18\x08\x0e\xf9\x0f{k\xac\x01\xc0\x16\x00(\x08\x93/(\x18\xd6J\xe3?\x18\x08\x0e\xf9\x0f{k\xac\x01\xc0\x00\x00\x00\x15\x04\x15`\x15dL\x15\x0c\x15\x04\x12\x00\x000\xbc\xb2j\n\x96\xcb\x01\x00\x00\x9d\xfd\xeb)_\x05\x00\x00YI\x00\xbf\xdc\x03\x00\x00\x88\x9b\xf7A\x03\x00\x00\x00!\xcb_\x83\x83\x00\x00\x00}\x85\x1ev\x8d\x03\x00\x00\x15\x00\x15\x16\x15\x1a,\x15\x0c\x15\x04\x15\x06\x15\x06\x1c\x18\x08\x9d\xfd\xeb)_\x05\x00\x00\x18\x08\x88\x9b\xf7A\x03\x00\x00\x00\x16\x00(\x08\x9d\xfd\xeb)_\x05\x00\x00\x18\x08\x88\x9b\xf7A\x03\x00\x00\x00\x00\x00\x00\x0b(\x02\x00\x00\x00\x0c\x01\x03\x03\x88\xc6\x02&\xd2\x0f\x1c\x15\x04\x195\x04\x00\x06\x19\x18\x11__index_level_0__\x15\x02\x16\x0c\x16\x8c\x02\x16\x94\x02&\xbe\x0e&\xbe\r\x1c\x18\x08\x9d\xfd\xeb)_\x05\x00\x00\x18\x08\x88\x9b\xf7A\x03\x00\x00\x00\x16\x00(\x08\x9d\xfd\xeb)_\x05\x00\x00\x18\x08\x88\x9b\xf7A\x03\x00\x00\x00\x00\x00\x00\x15\x02\x19l5\x00\x18\x06schema\x15\n\x00\x15\n%\x02\x18\x01A\x00\x15\n%\x02\x18\x01B\x00\x15\n%\x02\x18\x01C\x00\x15\n%\x02\x18\x01D\x00\x15\x04%\x02\x18\x11__index_level_0__\x00\x16\x0c\x19\x1c\x19\\&\x9c\x02\x1c\x15\n\x195\x04\x00\x06\x19\x18\x01A\x15\x02\x16\x0c\x16\x8c\x02\x16\x94\x02&\x88\x01&\x08\x1c\x18\x08l\x06_\xe7,\xfc\xcf?\x18\x08\xefG7G\t\xc7\x01\xc0\x16\x00(\x08l\x06_\xe7,\xfc\xcf?\x18\x08\xefG7G\t\xc7\x01\xc0\x00\x00\x00&\xc8\x05\x1c\x15\n\x195\x04\x00\x06\x19\x18\x01B\x15\x02\x16\x0c\x16\x8c\x02\x16\x94\x02&\xb4\x04&\xb4\x03\x1c\x18\x08v\x00\x87\xc6\\G\xeb?\x18\x08;_\xaeV\xe4\xa6\xdf\xbf\x16\x00(\x08v\x00\x87\xc6\\G\xeb?\x18\x08;_\xaeV\xe4\xa6\xdf\xbf\x00\x00\x00&\xf6\x08\x1c\x15\n\x195\x04\x00\x06\x19\x18\x01C\x15\x02\x16\x0c\x16\x8c\x02\x16\x94\x02&\xe2\x07&\xe2\x06\x1c\x18\x08f(\\\xe8d\xd3\xf8?\x18\x08\xa2\x95\x0f\n\x0c\x85\xde\xbf\x16\x00(\x08f(\\\xe8d\xd3\xf8?\x18\x08\xa2\x95\x0f\n\x0c\x85\xde\xbf\x00\x00\x00&\xa4\x0c\x1c\x15\n\x195\x04\x00\x06\x19\x18\x01D\x15\x02\x16\x0c\x16\x8c\x02\x16\x94\x02&\x90\x0b&\x90\n\x1c\x18\x08\x93/(\x18\xd6J\xe3?\x18\x08\x0e\xf9\x0f{k\xac\x01\xc0\x16\x00(\x08\x93/(\x18\xd6J\xe3?\x18\x08\x0e\xf9\x0f{k\xac\x01\xc0\x00\x00\x00&\xd2\x0f\x1c\x15\x04\x195\x04\x00\x06\x19\x18\x11__index_level_0__\x15\x02\x16\x0c\x16\x8c\x02\x16\x94\x02&\xbe\x0e&\xbe\r\x1c\x18\x08\x9d\xfd\xeb)_\x05\x00\x00\x18\x08\x88\x9b\xf7A\x03\x00\x00\x00\x16\x00(\x08\x9d\xfd\xeb)_\x05\x00\x00\x18\x08\x88\x9b\xf7A\x03\x00\x00\x00\x00\x00\x00\x16\xe4\n\x16\x0c&\x9c\x02\x16\xe4\n\x14\x00\x00\x19,\x18\x06pandas\x18\xa5\x06{"index_columns": ["__index_level_0__"], "column_indexes": [{"name": null, "field_name": null, "pandas_type": "unicode", "numpy_type": "object", "metadata": {"encoding": "UTF-8"}}], "columns": [{"name": "A", "field_name": "A", "pandas_type": "float64", "numpy_type": "float64", "metadata": null}, {"name": "B", "field_name": "B", "pandas_type": "float64", "numpy_type": "float64", "metadata": null}, {"name": "C", "field_name": "C", "pandas_type": "float64", "numpy_type": "float64", "metadata": null}, {"name": "D", "field_name": "D", "pandas_type": "float64", "numpy_type": "float64", "metadata": null}, {"name": null, "field_name": "__index_level_0__", "pandas_type": "int64", "numpy_type": "int64", "metadata": null}], "creator": {"library": "pyarrow", "version": "0.16.0"}, "pandas_version": "1.0.2"}\x00\x18\x0cARROW:schema\x18\xb8\x0c/////6AEAAAQAAAAAAAKAA4ABgAFAAgACgAAAAABAwAQAAAAAAAKAAwAAAAEAAgACgAAAFwDAAAEAAAAAQAAAAwAAAAIAAwABAAIAAgAAAAIAAAAEAAAAAYAAABwYW5kYXMAACUDAAB7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AeAAAAEwAAAAEAAAARP///wAAAQIkAAAAFAAAAAQAAAAAAAAACAAMAAgABwAIAAAAAAAAAUAAAAARAAAAX19pbmRleF9sZXZlbF8wX18AAACI////AAABAxgAAAAMAAAABAAAAAAAAAB2////AAACAAEAAABEAAAAsP///wAAAQMYAAAADAAAAAQAAAAAAAAAnv///wAAAgABAAAAQwAAANj///8AAAEDGAAAAAwAAAAEAAAAAAAAAMb///8AAAIAAQAAAEIAAAAQABQACAAGAAcADAAAABAAEAAAAAAAAQMgAAAAFAAAAAQAAAAAAAAAAAAGAAgABgAGAAAAAAACAAEAAABBAAAAAAAAAA==\x00\x18"parquet-cpp version 1.5.1-SNAPSHOT\x19\\\x1c\x00\x00\x1c\x00\x00\x1c\x00\x00\x1c\x00\x00\x1c\x00\x00\x00\xa0\x0b\x00\x00PAR1'
    uri = "ws://localhost:5000"

    async with websockets.connect(uri) as websocket:
        await websocket.send(data)
        recv_buffer_1 = await websocket.recv()

        await websocket.send(data)
        recv_buffer_2 = await websocket.recv()

    print(recv_buffer_1 == recv_buffer_2)

asyncio.get_event_loop().run_until_complete(hello())
/+ dub.sdl:
    name "server"
    dependency "arsd-official:cgi" version="5.3.8"
+/

import std;
import arsd.cgi;

void main() 
{
    writeln("Listening on port 5000");
    cgiMainImpl!(websocketEcho, Cgi, defaultMaxContentLength)(["--port", "5000"]);
}

void websocketEcho(Cgi cgi) {
    assert(cgi.websocketRequested(), "i want a web socket!");
    auto websocket = cgi.acceptWebsocket();

    while(websocket.recvAvailable(300.seconds)) 
    {
        auto msg = websocket.recv();
        if (msg.opcode == WebSocketOpcode.close) break;
        websocket.send(msg.data);

    }
    websocket.close();
}
adamdruppe commented 4 years ago

blargh i think it is being split up over multiple frames and not recombined. i did that code for my client side but looks like i never ported it over to the server side.

lemme see if i can, possibly will be forced to break the api tho

adamdruppe commented 4 years ago

ok try it now with the master branch.

andre2007 commented 4 years ago

Thanks that solved the issue.