GithubHelp home page GithubHelp logo

kripth / lighttp Goto Github PK

View Code? Open in Web Editor NEW
25.0 7.0 5.0 73 KB

Lightweight asynchronous HTTP/WS client/server

License: MIT License

D 99.12% HTML 0.88%
lightweight http ws websocket server client

lighttp's Introduction

lighttp Logo

DUB Package Build Status

Lighttp is a lightweight asynchronous HTTP and WebSocket server library for the D programming language with simple API.

import std.file;

import lighttp;

void main(string[] args) {

	Server server = new Server();
	server.host("0.0.0.0");
	server.host("::");
	server.router.add(new Router());
	server.router.add(Get("welcome"), new Resource("text/html", read("welcome.html")));
	server.run();

}

class Router {

	// GET /
	@Get("") getIndex(ServerResponse response) {
		response.body = "Welcome to lighttp!";
	}
	
	// GET /image/uhDUnsj => imageId = "uhDUnsj"
	@Get("image", "([a-zA-Z0-9]{7})") getImage(ServerResponse response, string imageId) {
		if(exists("images/" ~ imageId)) {
			response.contentType = MimeTypes.jpeg;
			response.body = read("images/" ~ imageId);
		} else {
			response.status = StatusCodes.notFound;
		}
	}

}

lighttp's People

Contributors

esakara avatar kripth avatar ripanioan avatar shove70 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar

lighttp's Issues

Unstable operation.

Description:

Running examples/static and trying to request with curl or browser, you will find that there is a certain probability of 400 Bad request errors. I did not track the cause of the problem carefully, but simply judged the probable point of the problem.

https://github.com/Kripth/lighttp/blob/master/src/lighttp/util.d#L480

In line 480 of util.d, we insert a line of code for observation and find that there is a high probability that an empty data parameter will be received for parse(). This data parameter should be one of the socket processing parameters.

MacOS 10.14.6, centos7 x64, dmd 2.088.0

Anyway to speed up compilation times?

Hello,

I noticed that the compiler spends a lot of time when it prints out:
Using Linux EPOLL for events

Is there a way to reduce this time?
What does this do?

too many open files.

Running examples / static, and then accessing it through browser or curl, you will find that the number of file handles opened by the system keeps increasing until an exception occurs beyond the operating system's limit on the number of files for a single process.

After checking, it was found that if isForce used the default parameter false when calling AsyncTCPConnection.kill(bool isForce = false) of libasync, it would not close the socket handle in time and truly, resulting in a straight increase in the number of connections.

Occasional out-of-range errors.

util.d line: 559
function: decodeHTTP()


core.exception.RangeError@/root/.dub/packages/lighttp-0.5.2/lighttp/src/lighttp/util.d(559): Range violation

??:? _d_arrayboundsp [0x6e432d]
/root/.dub/packages/lighttp-0.5.2/lighttp/src/lighttp/util.d:559 bool lighttp.util.decodeHTTP(immutable(char)[], ref immutable(char)[], ref lighttp.util.Http.Headers, ref immutable(char)[]) [0x676b27]
/root/.dub/packages/lighttp-0.5.2/lighttp/src/lighttp/util.d:482 bool lighttp.util.HttpImpl!(1, 0).HttpImpl.parse(immutable(char)[]) [0x6727f5]
/root/.dub/packages/lighttp-0.5.2/lighttp/src/lighttp/server/server.d:246 void lighttp.server.server.DefaultConnection.handleImpl(immutable(char)[]) [0x6708dd]
/root/.dub/packages/lighttp-0.5.2/lighttp/src/lighttp/server/server.d:226 void lighttp.server.server.DefaultConnection.handle() [0x6707a6]
/root/.dub/packages/lighttp-0.5.2/lighttp/src/lighttp/server/server.d:213 void lighttp.server.server.DefaultConnection.onRead() [0x670604]
/root/.dub/packages/lighttp-0.5.2/lighttp/src/lighttp/server/server.d:180 void lighttp.server.server.Connection.handle(libasync.tcp.TCPEvent) [0x67051e]
/root/.dub/packages/libasync-0.8.4/libasync/source/libasync/tcp.d:302 void libasync.tcp.TCPEventHandler.opCall(libasync.tcp.TCPEvent) [0x694a89]
/root/.dub/packages/libasync-0.8.4/libasync/source/libasync/posix.d:2622 nothrow bool libasync.posix.EventLoopImpl.onTCPTraffic(int, libasync.tcp.TCPEventHandler, int, libasync.tcp.AsyncTCPConnection) [0x68870d]
/root/.dub/packages/libasync-0.8.4/libasync/source/libasync/posix.d:549 nothrow bool libasync.posix.EventLoopImpl.loop(core.time.Duration).handleEvents() [0x682aa6]
/root/.dub/packages/libasync-0.8.4/libasync/source/libasync/posix.d:602 nothrow bool libasync.posix.EventLoopImpl.loop(core.time.Duration) [0x681c60]
/root/.dub/packages/libasync-0.8.4/libasync/source/libasync/events.d:302 nothrow bool libasync.events.EventLoop.loop(core.time.Duration) [0x679951]
/root/.dub/packages/lighttp-0.5.2/lighttp/src/lighttp/server/server.d:123 void lighttp.server.server.ServerBase.run() [0x67041c]
src/app.d:39 _Dmain [0x5ee360]

Cannot download files on chrome

@Get("download") getDownload(ServerRequest req, ServerResponse res) {
	res.headers["Content-Type"] = "application/json";
	res.headers["Content-Disposition"] = "attachment; filename=\"test.json\";";
	res.body = "{}";
}

Binary WebSocket Msgs

Currently Lighttp does not work with binary messages, only text messages. It assumes outgoing msgs are text, and ignores incoming binary msgs.

According to the WebSocket spec, found here, page 28:

  • %x1 denotes a text frame
  • %x2 denotes a binary frame

To change your code to work only with binary simply change:

  • In WebSocketConnection.send, line 341, from 0b10000001 to 0b10000010
  • In WebSocketConnection.onRead, line 314, from == 1 to == 2

Obviously that is not a good fix.

Vibe.d has a receiveBinary and a receiveText, and overloads send with const(char)[] and const(ubyte[]) respectively.

Given that Lighttp uses a callback style, I recommend adding overloads to WebSocketConnection.send and WebSocketConnection.onReceive. It will be a breaking change, given that the current binary-signatured functions will actually send binary, not text.

I can make a Pull Request. Any reason why WebSocketConnection.send takes a void[] and not a ubyte[]?

Log exceptions when thrown

Currently, if an exception is thrown in a user-provided handler, it will get silenced. The only trace of the exception will be the "500 Server Error" page and the error message nor stack trace are not recoverable.

The suggestion is to make them possible to log, and add a new option eg. printExceptions, preferably on by default. For example, handleRouteCatch could be expanded to include a stderr.writeln call:

	private void handleRouteCatch(ref HandleResult result, AsyncTCPConnection client, ServerRequest req, ServerResponse res) {
		try this.server.router.handle(this.server.options, result, client, req, res);
		catch(Exception ex) {
			import std.stdio;
			res.status = StatusCodes.internalServerError;
			stderr.writeln(ex);
		}
	}

Can self-assign.

chech websocket is not close before send

Writing Livereload in D,
When i Do

class Client : WebSocket {
....
void sendModify(string path){
try{
this.send( JSONValue([
"command":JSONValue("reload"),
"path":JSONValue(path),
"liveCSS":JSONValue(true)
]).toString()
);
}
catch(Exception e){
writeln("Erreur",e );
}
}

If page was closed, program quit (return error code 11) without throw exception

Any help ?
Big thanks

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.