GithubHelp home page GithubHelp logo

huntlabs / hunt-http Goto Github PK

View Code? Open in Web Editor NEW
32.0 5.0 5.0 1.89 MB

http library for D, support http 1.1 / http 2.0 (http2) / websocket server and client.

License: Apache License 2.0

D 100.00%
http-protocol http2 http-server http-client websocket hunt

hunt-http's Introduction

Build Status

hunt-http

Hunt-Http is a flexible performant http server and client written in D Programming Language.

Features

Feature Server Client
HTTP 1.x tested tested
HTTP/2 tested tested
TLS 1.2 tested tested[1]
WebSocket[2] tested tested
Routing tested none
Cookie tested tested[3]
Session tested[4] tested
Form-Data[5] tested tested
X-WWW-Form tested tested

Note:

[1] Mutual TLS supported
[2] WSS untested
[3] In-memory only
[4] In-memory only
[5] File upload and download

Simple codes

Using hunt-http build a web server

import hunt.http;

void main()
{
    auto server = HttpServer.builder()
        .setListener(8080, "0.0.0.0")
        .setHandler((RoutingContext context) {
            context.write("Hello World!");
            context.end();
        }).build();

    server.start();
}

Using hunt-http build a http client

import hunt.http;

import std.stdio;

void main()
{
    auto client = new HttpClient();

    auto request = new RequestBuilder().url("http://www.huntlabs.net").build();

    auto response = client.newCall(request).execute();

    if (response !is null)
    {
        writeln(response.getBody().asString());
    }
}

Using hunt-http build a websocket server

import hunt.http;

void main()
{
    auto server = HttpServer.builder()
        .setListener(8080, "0.0.0.0")
        .websocket("/ws", new class AbstractWebSocketMessageHandler {
            override void onText(WebSocketConnection connection, string text)
            {
                connection.sendText("Hello " ~ text);
            }
        }).build()

    server.start();
}

Avaliable versions

Name Description
HUNT_HTTP_DEBUG Used to log debug messages about Hunt-HTTP
HUNT_METRIC Used to enable some operations and APIs about metric

TODO

  • Reorganizing modules
  • PersistentCookieStore for HttpClient
  • Benchmark

References

hunt-http's People

Contributors

heromyth avatar xiaoshuaisen avatar zhangyuchun avatar zoujiaqing 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  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar

hunt-http's Issues

Serve static files

How do I serve static files with hunt-http? Assume that I have a directory with an index.html, some other HTML files, images and a CSS file. No dynamic content. Thanks for helping!

https client

sample program, client requesting an https resource :

import hunt.http;
import std.stdio;

void main()
{
    auto client = new HttpClient();
    auto request = new RequestBuilder().url("https://www.google.com").build();
    auto response = client.newCall(request).execute();
    if (response !is null)
    {
        writeln(response.getBody().asString());
    }
}

gives error

2020-May-31 09:36:05.8637941 | 7341 | warning | RealCall.execute | No any response in 15 secs | ../../../.dub/packages/hunt-http-0.6.0/hunt-http/source/hunt/http/client/RealCall.d:199
hunt.Exceptions.TimeoutException@../../../.dub/packages/hunt-http-0.6.0/hunt-http/source/hunt/http/client/RealCall.d(205)
----------------
../../../.dub/packages/hunt-http-0.6.0/hunt-http/source/hunt/http/client/RealCall.d:205 hunt.http.client.HttpClientResponse.HttpClientResponse hunt.http.client.RealCall.RealCall.execute() [0x55b790b27769]
source/app.d:9 _Dmain [0x55b790b19d3a]
Program exited with code 1

$ cat dub.selections.json
{
        "fileVersion": 1,
        "versions": {
                "hunt": "1.6.1",
                "hunt-extra": "1.0.0",
                "hunt-http": "0.6.0",
                "hunt-net": "0.5.0",
                "hunt-openssl": "1.0.1"
        }
}

Improve HttpClient easier to use.

Like this code to GET:

import std.stdio;
import hunt.http.client;

void main()
{
    string url = "https://www.huntlabs.net/";

    HttpClient client = new HttpClient();

    Request request = new Request.builder()
      .url(url)
      .build();

    Response response = client.newCall(request).execute();
    writeln( response.body().string() );
}

Like this code to POST:

import std.stdio;
import hunt.http.client;

void main()
{
    string url = "https://www.huntlabs.net/";

    HttpClient client = new HttpClient();

    RequestBody body = RequestBody.create(MediaType.get("application/json; charset=utf-8"), "{\"hello\": \"world!\" }");
    Request request = new Request.builder()
      .url(url)
      .post(body)
      .build();

    Response response = client.newCall(request).execute();
    writeln( response.body().string() );
}

Error: library contain multiple objects of the same name

$ dub --arch=x86_64
Performing "debug" build using C:\D\dmd2\windows\bin\dmd.exe for x86_64.
hunt 1.1.0: target for configuration "library" is up to date.
hunt-security 0.1.0: target for configuration "library" is up to date.
hunt-net 0.1.0: target for configuration "default" is up to date.
protobuf 0.4.0: target for configuration "protobuf" is up to date.
hunt-imf 0.0.5: target for configuration "hunt-imf" is up to date.
hunt-trace 0.1.8: target for configuration "library" is up to date.
hunt-http ~master: target for configuration "default" is up to date.
httpserverdemo ~master: building configuration "application"...
Linking...
hunt-http.lib(Frame.obj) : warning LNK4255: library contain multiple objects of the same name; linking object as if no debug info LINK : fatal error LNK1104: cannot open file '..\..\..\..\..\AppData\Local\dub\packages\hunt-security-0.1.0\hunt-security\.dub\build\library-debug-windows-x86_64-dmd_2084-8DC22091CDB34FC108BE2446A168BB0E\hunt-security.lib'
Error: linker exited with status 1104
C:\D\dmd2\windows\bin\dmd.exe failed with exit code 1.

Windows 10
v2.084.0

Improvment HttpServer are easier to use.

Like this code:

import hunt.http;

void main()
{
    auto server = new HttpServer;

    server.listener(8080, "localhost");

    /*

    onGet

    onPost

    onPut

    onDelete

    onRequest

    */

    server.onGet("/", (RoutingContext ctx) {
        // TODO
    });

    server.onPost("/login", (RoutingContext ctx) {
        // TODO
    });

    server.onRequest(HttpMethod.GET, "/login", (RoutingContext ctx) {
        // TODO
    });

    server.websocket("/ws", new class AbstractWebSocketMessageHandler {
        override void onOpen(WebSocketConnection connection) {}
        override void onText(WebSocketConnection connection, string text) {}
    });

    server.resource("/static-files", new class AbstractResourceHandler("/www/website/public") {});

    server.setDefaultRequest((RoutingContext ctx) {
        ctx.status(404);
        ctx.send("Not Found");
        ctx.write("!");
        ctx.end();
    });

    server.start();
}

How to get Context object?

exmple code:

void main()
{
    auto httpserver = new HttpServer;
    httpserver.get("/test").handler((Context ctx) {

        auto response = new Response;
        response.setBody("Hello world!");

        ctx.setResponse(response);
        ctx.done();
    });
}

But! What is Context object?

Building failed on Mac OS

The error message:

Linking...
dmd -of.dub/build/server-debug-posix.osx-x86_64-dmd_2091-789ACCFB4E7175AA7E03295664C71631/http-server .dub/build/server-debug-posix.osx-x86_64-dmd_2091-789ACCFB4E7175AA7E03295664C71631/http-server.o ../../.dub/build/default-debug-posix.osx-x86_64-dmd_2091-5E13BCCCBAA4897920B997E7C0DE09B6/libhunt-http.a ../../../hunt-net/.dub/build/default-debug-posix.osx-x86_64-dmd_2091-8715AC4D8BCF54BB913AF1A6B5D68845/libhunt-net.a ../../../hunt/.dub/build/library-debug-posix.osx-x86_64-dmd_2091-D2D93C3E043B34896F959408A5A7FD54/libhunt.a -m64 -g
ld: in ../../.dub/build/default-debug-posix.osx-x86_64-dmd_2091-5E13BCCCBAA4897920B997E7C0DE09B6/libhunt-http.a(concurrency_5c25_41b.o), in section __TEXT,__textcoal_nt reloc 2: symbol index out of range
clang: error: linker command failed with exit code 1 (use -v to see invocation)
Error: linker exited with status 1

Here is the bug: https://issues.dlang.org/show_bug.cgi?id=19044

Hunt-http does not compile on linux .

I wanted to use web sockets but dependency hunt-web did not exists.
Than I cloned hunt-web and run dub build myself but it seems hunt-http is not compiling neither.

Ubuntu 18.04
DMD64 D Compiler v2.089.0
hunt-web = master
hunt-http = 0.3
Error :
erdem@erdem-HP-EliteDesk-800-G2-TWR ~/dev/hunt/hunt-web (master) $ dub build
Performing "debug" build using /usr/bin/dmd for x86_64.
hunt-web 1.0.0-beta.1: building configuration "default"...
../../../.dub/packages/hunt-http-0.3.0/hunt-http/source/hunt/http/client/package.d(1,1): Error: module hunt.http.client from file ../../../.dub/packages/hunt-http-0.3.0/hunt-http/source/hunt/http/client/package.d conflicts with package name client
/usr/bin/dmd failed with exit code 1.

Erdem

Build failed on Windows 10

hunt 1.7.9: target for configuration "library" is up to date.
hunt-extra 1.1.5: target for configuration "library" is up to date.
hunt-net 0.6.6: building configuration "default"...
Unwind edges out of a funclet pad must have the same unwind dest
  %15 = cleanuppad within none []
  cleanupret from %15 unwind to caller
  invoke x86_vectorcallcc void @"\01_D4hunt4util4pool10ObjectPool__TQpTCQBi5event9EventLoopQkZQBp13handleWaitersMFZv"(%"hunt.util.pool.ObjectPool.ObjectPool!(EventLoop).ObjectPool"* nonnull %27) #1 [ "funclet"(token %15) ]
          to label %28 unwind label %53, !dbg !915
in function �_D4hunt4util4pool10ObjectPool__TQpTCQBi5event9EventLoopQkZQBp12returnObjectMFQBqZv
LLVM ERROR: Broken function found, compilation aborted!
0x00007FF67D717776 (0x000000A9ECFFD4B8 0x00007FF67F88CD93 0x0000000000000016 0x00007FF67D717770)
0x00007FF67F88CC47 (0x000001F18E7A6D01 0x000000A900000000 0x0000000000000000 0x000000A9ECFFD560)
0x00007FF67F880C14 (0x000001F100000002 0x000000A9ECFFD560 0x0000000000000000 0x000001F192E59F68)
0x00007FF67D2E6816 (0x000000A9ECFFD638 0x0A007FF680D4C938 0x000048B0557FC67A 0x000000A9ECFFD638)
0x00007FF67D2E6627 (0x0000000000000000 0x00007FF680D653A8 0x000048B0557FC63A 0x0000000000000000)
0x00007FF67D73E0F0 (0x0000000000000601 0x0000000000000000 0x0000000000000000 0x00007FF67D492E9C)
0x00007FF67D6D3709 (0x000001F1E7833840 0x000000A9ECFFDD68 0x000000A9ECFFD860 0x00007FF67D2AF0D4)
0x00007FF67D6D2CAE (0x0000000000000000 0x00007FF67DD51ABC 0x000000A9ECFFD978 0x0000000000000000)
0x00007FF67D6D8FF4 (0x000001F192FA6C00 0x000000A900000000 0x000001F192FA6B78 0x000001F192FA6B00)
0x00007FF67F804AE3 (0x000001F192E536C0 0x0000000000000018 0x0000000000000000 0x0000000000000000)
0x00007FF67F7D91D7 (0x000000A9ECFFE3A8 0x000000000007D097 0x000001F1E5BC0FC0 0x0000000000000001)
0x00007FF67F74606F (0x000000000000003F 0x000000000000003F 0x000001F1E76A9900 0x0000000000000005)
0x00007FF67F6DC803 (0x0000000000000000 0x00007FFF00000000 0x0000000000000000 0x0000000000000000)
0x00007FF67F7433DF (0x002E003200000000 0x000001F15F00005F 0x00007FF67D717A3D 0x0000000000000026)
0x00007FF67F843E99 (0x0000000300000002 0x0000000000100000 0x00007FF680BA4513 0x0000000000000000)
0x00007FF67F843B2D (0x000001F1E59F69C8 0x000000A9ECFFFC18 0x000000A9ECFFFA40 0x0000000000000001)
0x00007FF67F843E24 (0x000048B000000059 0x00007FFF00000000 0x000001F1E59F6A16 0x0000000000000059)
0x00007FF67F73EE29 (0x0000000000000000 0x0000000000000000 0x0000000000000000 0x0000000000000000)
0x00007FF67F86F704 (0x0000000000000000 0x0000000000000000 0x0000000000000000 0x0000000000000000)
0x00007FFF379E7034 (0x0000000000000000 0x0000000000000000 0x0000000000000000 0x0000000000000000), BaseThreadInitThunk() + 0x14 bytes(s)
0x00007FFF37EC2651 (0x0000000000000000 0x0000000000000000 0x0000000000000000 0x0000000000000000), RtlUserThreadStart() + 0x21 bytes(s)
C:\Program Files\LDC 1.26\bin\ldc2.exe failed with exit code -1073741795.```

template Completable is not defined

Using this dub.sdl:

dependency "hunt-http" version="~>0.2.2"

and this app.d:

import hunt.http.client;

Gives this result:

$ dub build
Performing "debug" build using C:\dmd2\windows\bin\dmd.exe for x86_64.
hunt 1.2.2: target for configuration "library" is up to date.
hunt-http 0.2.2: building configuration "library"...
C:\Users\Steven\AppData\Local\dub\packages\hunt-http-0.2.2\hunt-http\source\
hunt\http\client\HttpClient.d(84,5): Error: template instance
`Completable!HttpClientConnection` template Completable is not defined

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.