使用D,我将如何监听传入的HTTP请求并对其进行响应? [英] Using D, how would I listen to incoming HTTP requests and respond to them?

查看:209
本文介绍了使用D,我将如何监听传入的HTTP请求并对其进行响应?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用D,我将如何监听传入的HTTP流量并对其进行响应?

Using D, how would I listen to incoming HTTP traffic and respond to it?

例如(伪代码):

socket = new socket("locahost", 80)
socket.onRequestRecevied(handleRequest);

function response handleRequest(request) {
  //do something with the request and  respond
  request.respond("hello world")
}

我知道它还有很多东西,但我还没有找到很多资源来回复对于传入的http请求。

I know there is a lot more to it than that, but I haven't been able to find many resources on responding to incoming http request.

编辑:
我当前的尝试只产生了无法创建套接字:操作不允许等异常。这可能意味着我正确地做了但只是收到系统错误信息。

My current attempts have yielded only exceptions like "Unable to create socket: Operation not permitted." which may mean I'm doing it correctly but am simply receiving a system error message.

推荐答案

它与听正常情况相同传入的TCP连接并响应它们:

it's the same as listening to normal incoming TCP connections and responding to them:

  import std.socket;
  Socket s = new TcpSocket(AddressFamily.INET);
  s.bind(new InternetAddress("0.0.0.0", 80));
  s.listen(8);
  while (true) {
    Socket conn=s.accept();
    //handle incoming message (I'm putting it in a task pool to handle ;) )
    taskPool.put(task!handleHttp(conn));
  }

handleHttp(套接字)包含接收http请求的逻辑并按照http标准发送响应(你必须找到你的自己)

with handleHttp(Socket) containing the logic for receiving the http request and sending the response as defined be the http standard (you'll have to find that your self though)

这篇关于使用D,我将如何监听传入的HTTP请求并对其进行响应?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆