2016-01-27 14 views
1

documentation:“目前没有提供用于解析(或渲染)字符串或字节数组的公共API”。如何使用私有API将网络流解析为HttpRequest对象?如何使用akka-http将字节流解析为`HttpRequest`对象?

此外,应输入字节流是什么呢?

  1. 无序的数据包(以太网及以上)
  2. 无序的TCP数据包
  3. 有序的TCP有效载荷(HTTP原料要求,按顺序)(这是​​我的猜测)

注:

  1. 我的字节流将只包含http请求,没有响应
  2. 我GET来自pcap文件的这个字节流,而不是来自实时http客户端。 (这也是为什么我问我需要多少把它发送到阿卡,http前面解析它)
  3. 如果你认为这是一个坏主意,请解释一下,这将是一个很大的帮助了。
  4. 我怀疑这涉及到使用http()。serverLayer。我不确定。

link:在谷歌阿卡集团

感谢一个相关的问题!

+0

你的问题是什么? – TNW

+0

@TNW,我特别编辑了我的问题。你怎么看? – Hibuki

+0

下面是[一些](https://github.com/akka/akka/blob/f008a932c381013f6060ee00a731862bafed2be7/akka-http-core/src/test/scala/akka/http/impl/engine/client/TlsEndpointVerificationSpec.scala)从阿卡-HTTP单元的例子](https://github.com/akka/akka/blob/f008a932c381013f6060ee00a731862bafed2be7/akka-http-core/src/test/scala/akka/http/impl/engine/client/TlsEndpointVerificationSpec.scala)测试,他们可能会帮助 – Hibuki

回答

0

这是可能的,但很费劲。我已经这样做了,但是我不能在这里发布完整的代码,因为它是私人的。

相关AKKA代码确实这是HttpRequestParser

大部分的代码是包私有的,所以你需要编写一个辅助类自己在akka.http包去访问它。

该代码将是这个样子:

package akka.http 

/** 
* This class gives us access to Akka's [[akka.http.impl.engine.parsing.HttpRequestParser]]. 
* 
* It is in the akka package, as most of Akka's parsing code is package-private. 
* 
* @see [[akka.http.impl.engine.server.HttpServerBluePrint]] 
*/  
class HttpRequestParserHelper()(
    implicit system: ActorSystem, 
    materializer: Materializer) { 

    def unmarshalRequest(wireFormat: Array[Byte]): HttpRequest = { 

    val input = ByteString(wireFormat) 

    val requestFuture = Source.single(input) 
     .via(requestParsingFlow()) 
     .runWith(Streams.getSingleElement) 
     .transform(identity, e => 
     new IOException("Error unmarshalling request", e)) 

    Await.result(requestFuture, 10.seconds) 
    } 

    /** 
    * An Akka Flow which parses incoming ByteStrings and emits HttpRequests. 
    * 
    * This code was copied from [[akka.http.impl.engine.server.HttpServerBluePrint]] 
    */ 
    private def requestParsingFlow(): Flow[ByteString, HttpRequest, Any] = { 
    ... 
    } 
} 

填写requestParsingFlow从AKKA源代码,删除直到它编译这是不适用的位。

输入数据需要从你的问题是TCP流的数据,即选项(3)。

祝你好运!

相关问题