葡京网上娱乐场Akka(38): Http:Entityof ByteString-数据传基础

自适应宽度是凭借当不明显设定容器的升幅(或异地距设为auto)时,在特定的情景下容器的宽度会因事态自行设定,而设定的结果往往并无是咱纪念要之。

 
我们说罢Akka-http是一个吓的系统并工具,集成是透过数量交换方式实现之。Http是个在网上传和接的正规化商事。所以,在使Akka-http之前,可能咱们还是待将Http模式的网上数据交换细节了解了解。数据交换双方是经Http消息类型Request和Response来促成的。在Akka-http中对应之是HttpRequest和HttpResponse。这片独品类且持有HttpEntity类型来装需要交换的数额。首先,无论如何数据在线上的表现形式肯定是一串bytes。所以,数据交换两头Request,Response中之Entity也必是坐bytes来发挥的。在Akka-http里我们管用导的数码易成ByteString,通过网发送給接收端、接收端再将接受消息Entity中的ByteString转换成为靶子项目的数据。这半单易过程尽管是Akka-http的Marshalling和Unmarshalling过程了。我们先行由HttpEntity的构建函数来打听它的概念:

W3C规范中讲述了几栽shrink-to-fit的情景

  • 10.3.5 Floating, non-replaced
    elements
  • 10.3.7 Absolutely positioned, non-replaced
    elements
  • 10.3.8 Absolutely positioned, replaced
    elements
  • 10.6.4 Absolutely positioned, non-replaced
    elements

正规被提到了一个基本概念,我们事先来询问一下。

object HttpEntity {
  implicit def apply(string: String): HttpEntity.Strict = apply(ContentTypes.`text/plain(UTF-8)`, string)
  implicit def apply(bytes: Array[Byte]): HttpEntity.Strict = apply(ContentTypes.`application/octet-stream`, bytes)
  implicit def apply(data: ByteString): HttpEntity.Strict = apply(ContentTypes.`application/octet-stream`, data)
  def apply(contentType: ContentType.NonBinary, string: String): HttpEntity.Strict =
    if (string.isEmpty) empty(contentType) else apply(contentType, ByteString(string.getBytes(contentType.charset.nioCharset)))
  def apply(contentType: ContentType, bytes: Array[Byte]): HttpEntity.Strict =
    if (bytes.length == 0) empty(contentType) else apply(contentType, ByteString(bytes))
  def apply(contentType: ContentType, data: ByteString): HttpEntity.Strict =
    if (data.isEmpty) empty(contentType) else HttpEntity.Strict(contentType, data)

  def apply(contentType: ContentType, contentLength: Long, data: Source[ByteString, Any]): UniversalEntity =
    if (contentLength == 0) empty(contentType) else HttpEntity.Default(contentType, contentLength, data)
  def apply(contentType: ContentType, data: Source[ByteString, Any]): HttpEntity.Chunked =
    HttpEntity.Chunked.fromData(contentType, data)
...

replaced elements & non-replaced elements

css把html元素分为了点滴好像:不可替换元素和可替换元素。

  • 1.可替换元素

CSS 里,可替换元素是这般有些因素,
其呈现不是由CSS来控制的。这些外部因素的展现不依靠让CSS规范。
典型的只是替换元素来 img、 object、 video 以及
textarea、input这样的表单元素。 一些因素,比如audio和 canvas
,只于片例外情况下是不过替换元素。 使用了 CSS content
属性插入的对象被誉为匿名的只是替换元素。

  • 2.不可替换元素

恰恰相反,则也不可替换元素。

了解了概念后,我们回归正题。shrink-to-fit的景况有多,这里介绍一种最普遍的动静,即不得代替元素浮动时的自适应宽度(Floating,
non-replaced
elements),听起来有点抽象,但你或时时碰到。先看一个例:

html&css

<!DOCTYPE html>
<html>
    <style type="text/css">
        .outer {
            width: 800px;
            background: black;
            overflow: hidden;
        }
        .inner {
            float: left;
            background: red;
        }
        .sub1 {
            width: 26%;
            background: blue;
        }
        .sub2 {
            width: 50%;
            background: green;
        }
    </style>
<head>
</head>
<body>
    <div class="outer">
        <div class="inner">
            <div class="sub1">
                this is 1th line this is 2th line this is 3th line this is 4th line
            </div>
            <div class="sub2">
                sub2 block
            </div>

        </div>
    </div>
</body>
</html>

随即段样式定义了一个outer容器,背景啊黑色还增幅为800px,它其中还有一个里容器inner,无宽度且左弯,inner里产生少数只稍片sub1和sub2。

那么问题来了,请问inner,sub1,sub2具体的宽度为多少?

先行押成效图再次回复:

葡京网上娱乐场 1

结果应会胜出你的预想:inner(红色背景)的增长率并无是outer(黑色背景)的宽度(正常状态下应该可以继承父容器的幅度),因而sub1和sub2较我们预料的假设聊得差不多。

再度对这个题材之前,我们先行计算修改一下,看是否从中找到出现这个题材的来头。经过调试,发现有限栽最简易的方案可以化解这题目:

  • 1.给inner加宽度width: 100%;

  • 2.取消inner的浮动。

化解后的功用如下:

葡京网上娱乐场 2

当即诚然是咱想只要的,可即也巧妙地’躲’过了不可替换元素浮动这个场景。老实说,我频繁相逢过是状况,但是仅仅也就算是使上述两个方案去品尝,可并无懂得真正的原故,于是还是咋了一下W3C有关这上头的正规,规范之叙说如下:

葡京网上娱乐场 3

先是不说英文的题材,单纯的’Roughly’和‘CSS 2.1 does not define the exact
algorithm’这片词就为人口左右为难,然后还吃来了shrink-to-fit的一个公式:

min(max(preferred minimum width, available width), preferred width)

呵呵,然并卵啊,天喻这三单价值怎么竟。

再也网上google一下,发现多人数还撞这个题目,但也是读不晓规范,也有人拿上面一样截翻译了瞬间,大家可以看看:

CSS2.1 并未给出 preferred minimum width、available width 和 preferred width 确切算法,通常,将内容中非明确的换行外的其他部分强制不换行来计算 preferred width;反之,尝试将内容尽可能的换行,以得到 preferred minimum width;available width 即该元素的包含块的宽度减去 'margin-left','border-left-width','padding-left','padding-right','border-right-width','margin-right' 的值以及任何存在的纵向滚动条的宽度。

既于当下段翻译绕晕的要举手。。。。。。。。。。。。。

重回归正题,经过近一个小时之找,终于为自己将当时段难理解的英文捋顺了:

此地发出三只参数,分别吗:preferred minimum width, available width,
preferred width.只待关注preferred width的计量方法即可,preferred
width的乘除办法如下:

让元素内容强制不换行后的最大宽度即为shrink-to-fit后的宽度

实际将点的事例,inner中的sub1的始末由宽度不够吃换行了,将那挟持不换行算有之增幅就是inner自适应之涨幅(inner本身没如宽度喔~),如何强制不换行也很粗略,慢慢的将sub1的宽度调大,就会见发现调到100%时不时正够用一行来具体其内容,这时内容之宽就是inner自适应后底增幅。直接上图:

葡京网上娱乐场 4

总结:对于浮动或者特殊的定位方式,推荐显式的设置容器宽度,避免出现意想不到的结果

翻开Blog原文请戳此处

雅显著,HttpEntity可以划分点儿深接近,一栽是Strict类型的,它的data是ByteString。另一样栽是UniversalEntity类型,它的数目dataBytes是Source[ByteString,Any]。无论如何最终在线上的还是ByteString。HttpEntity的ContentType注明了传输数据格式,有:

object ContentTypes {
  val `application/json` = ContentType(MediaTypes.`application/json`)
  val `application/octet-stream` = ContentType(MediaTypes.`application/octet-stream`)
  val `text/plain(UTF-8)` = MediaTypes.`text/plain` withCharset HttpCharsets.`UTF-8`
  val `text/html(UTF-8)` = MediaTypes.`text/html` withCharset HttpCharsets.`UTF-8`
  val `text/xml(UTF-8)` = MediaTypes.`text/xml` withCharset HttpCharsets.`UTF-8`
  val `text/csv(UTF-8)` = MediaTypes.`text/csv` withCharset HttpCharsets.`UTF-8`

  // used for explicitly suppressing the rendering of Content-Type headers on requests and responses
  val NoContentType = ContentType(MediaTypes.NoMediaType)
}

注意:ContentType只是均等种备注,不影响线达数表达形式,线上的数额永远是ByteString。但是,其中的application/octet-stream类型代表数量必须是Source[ByteString,Any]。我们下就是经过客户端的事例来明HttpEntity。下面是一个客户端测试函数:

  def runService(request: HttpRequest, rentity: RequestEntity) = {
   val futResp = for {
     entity <- Future.successful(rentity)
     resp <- Http(sys).singleRequest(
       request.copy(entity = rentity)
     )
   } yield resp

   futResp
    .andThen {
      case Success(r@HttpResponse(StatusCodes.OK, _, entity, _)) =>
        entity.dataBytes.map(_.utf8String).runForeach(println)
      case Success(r@HttpResponse(code, _, _, _)) =>
        println(s"Download request failed, response code: $code")
        r.discardEntityBytes()
      case Success(_) => println("Unable to download rows!")
      case Failure(err) => println(s"Download failed: ${err.getMessage}")

    }
  }

咱俩就待对这个函数传入RequestEntity就足以了解返回Response里Entity的成百上千细节了。首先我们渴求服务端发送一个纯字符串Hello
World。服务端代码如下:

 } ~ path("text") {
      get {
        complete("Hello World!")
      } ~

虽然complete(“Hello
World!”)有些迷糊,不过应complete做了来字符串到ByteString的更换。我们可以从上面这个runService函数得到证实。下面是是事例的调用:

  val reqText = HttpRequest(uri = s"http://localhost:8011/text")
  runService(reqText,HttpEntity.Empty)
    .andThen{case _ => sys.terminate()}

从显示的结果好得出runService函数中之entity.dataBytes.map(_.utf8String)已经将ByteString转换成为了String,也就是说服务器端发送的Entity里的数目是ByteString。

咱还尝试着发送一些数量給服务端,然后叫服务端把结果通过response
entity返回来:

    } ~ path("text") {
      get {
        complete("Hello World!")
      } ~
        post {
          withoutSizeLimit {
            extractDataBytes { bytes =>
              val data = bytes.runFold(ByteString())(_ ++ _)
              onComplete(data) { t =>
                complete(t)
              }
            }
          }
        }

咱看服务端对request
entity的操作是以ByteString进行的。客户端上污染一模一样失误字符的request如下:

  val postText = HttpRequest(HttpMethods.POST,uri = s"http://localhost:8011/text")
  val uploadText = HttpEntity(
    ContentTypes.`text/plain(UTF-8)`,
    // transform each number to a chunk of bytes
    ByteString("hello world again")
  )
  runService(postText,uploadText)
    .andThen{case _ => sys.terminate()}

咱得以视放上entity里的多寡是ByteString。

咱俩领略Akka-http是根据Akka-Stream的,具备Reactive-Stream功能特色。下面我们尽管示范一下哪些进展stream的上传下载。首先定制一个Source:

  val numbers = Source.fromIterator(() =>
    Iterator.continually(Random.nextInt()))
    .map(n => ByteString(s"$n\n"))
  //make conform to withoutSizeLimit constrain
  val source = limitableByteSource(numbers)

服务端也是为此HttpEntity来装这个Source然后经HttpRequest传被客户端的:

  path("random") {
      get {
        complete(
          HttpEntity(
            ContentTypes.`application/octet-stream`,
            // transform each number to a chunk of bytes
            source.take(10000)
          )
        )
      } ~

咱俩当客户端还是用runService来分析传过来的entity。由于接一个巨型的Source,所以用修改一下接到方式代码:

   futResp
    .andThen {
      case Success(r@HttpResponse(StatusCodes.OK, _, entity, _)) =>
        val futEnt = entity.dataBytes.map(_.utf8String).runForeach(println)
             Await.result(futEnt, Duration.Inf) // throws if binding fails
             println("End of stream!!!")
      case Success(r@HttpResponse(code, _, _, _)) =>
        println(s"Download request failed, response code: $code")
        r.discardEntityBytes()
      case Success(_) => println("Unable to download rows!")
      case Failure(err) => println(s"Download failed: ${err.getMessage}")

    }

就此底的计调用:

  val reqRandom = HttpRequest(uri = s"http://localhost:8011/random")
    runService(reqRandom,HttpEntity.Empty)
     .andThen{case _ => sys.terminate()}

再度示范一下当客户端用Source上污染数据。服务端代码:

       post {
          withoutSizeLimit {
            extractDataBytes { bytes =>
              val data = bytes.runFold(ByteString())(_ ++ _)
              onComplete(data) { t =>
                complete(t)
              }
            }
          }
        }

客户端上污染数据范例:

 val numbers = Source.fromIterator(() =>
    Iterator.continually(Random.nextInt()))
    .map(n => ByteString(s"$n\n"))
  //make conform to withoutSizeLimit constrain
  val source = limitableByteSource(numbers)

  val bytes = HttpEntity(
    ContentTypes.`application/octet-stream`,
    // transform each number to a chunk of bytes
    source.take(10000)
  )
  val postRandom = HttpRequest(HttpMethods.POST,uri = s"http://localhost:8011/random")
  runService(postRandom,bytes)
    .andThen{case _ => sys.terminate()}

从今者讨论我们询问了在Marshal,Unmarshal下层才是ByteString的操作及换。下面是此次讨论示范源代码:

服务端:

import akka.actor._
import akka.stream._
import akka.stream.scaladsl._
import akka.http.scaladsl.Http
import akka.http.scaladsl.server.Directives._
import akka.http.scaladsl.model._
import akka.util.ByteString
import akka.http.scaladsl.model.HttpEntity._

import scala.util.Random

object ServerEntity extends App {

  implicit val httpSys = ActorSystem("httpSystem")
  implicit val httpMat = ActorMaterializer()
  implicit val httpEC = httpSys.dispatcher


  val numbers = Source.fromIterator(() =>
    Iterator.continually(Random.nextInt()))
    .map(n => ByteString(s"$n\n"))
  //make conform to withoutSizeLimit constrain
  val source = limitableByteSource(numbers)



  val route =
    path("random") {
      get {
        withoutSizeLimit {
          complete(
            HttpEntity(
              ContentTypes.`application/octet-stream`,
              // transform each number to a chunk of bytes
              source.take(1000))
          )
        }
      } ~
        post {
          withoutSizeLimit {
            extractDataBytes { bytes =>
              val data = bytes.runFold(ByteString())(_ ++ _)
              onComplete(data) { t =>
                complete(t)
              }
            }
          }
        }
    } ~ path("text") {
      get {
        complete("Hello World!")
      } ~
        post {
          withoutSizeLimit {
            extractDataBytes { bytes =>
              val data = bytes.runFold(ByteString())(_ ++ _)
              onComplete(data) { t =>
                complete(t)
              }
            }
          }
        }
    }


  val (port, host) = (8011,"localhost")

  val bindingFuture = Http().bindAndHandle(route,host,port)

  println(s"Server running at $host $port. Press any key to exit ...")

  scala.io.StdIn.readLine()

  bindingFuture.flatMap(_.unbind())
    .onComplete(_ => httpSys.terminate())

}

客户端:

import akka.actor._
import akka.stream._
import akka.stream.scaladsl._
import akka.http.scaladsl.Http
import akka.http.scaladsl.model.HttpEntity.limitableByteSource
import akka.http.scaladsl.model._

import scala.concurrent.duration._
import akka.util.ByteString

import scala.concurrent._
import scala.util._

object ClientEntity extends App {

  implicit val sys = ActorSystem("ClientSys")
  implicit val mat = ActorMaterializer()
  implicit val ec = sys.dispatcher

  def runService(request: HttpRequest, rentity: RequestEntity) = {
   val futResp = for {
     entity <- Future.successful(rentity)
     resp <- Http(sys).singleRequest(
       request.copy(entity = rentity)
     )
   } yield resp

   futResp
    .andThen {
      case Success(r@HttpResponse(StatusCodes.OK, _, entity, _)) =>
        val futEnt = entity.dataBytes.map(_.utf8String).runForeach(println)
             Await.result(futEnt, Duration.Inf) // throws if binding fails
             println("End of stream!!!")
      case Success(r@HttpResponse(code, _, _, _)) =>
        println(s"Download request failed, response code: $code")
        r.discardEntityBytes()
      case Success(_) => println("Unable to download rows!")
      case Failure(err) => println(s"Download failed: ${err.getMessage}")

    }
  }

  val reqText = HttpRequest(uri = s"http://localhost:8011/text")
//  runService(reqText,HttpEntity.Empty)
//    .andThen{case _ => sys.terminate()}

  val postText = HttpRequest(HttpMethods.POST,uri = s"http://localhost:8011/text")
  val uploadText = HttpEntity(
    ContentTypes.`text/plain(UTF-8)`,
    // transform each number to a chunk of bytes
    ByteString("hello world again")
  )
//  runService(postText,uploadText)
//    .andThen{case _ => sys.terminate()}

  val reqRandom = HttpRequest(uri = s"http://localhost:8011/random")
 //   runService(reqRandom,HttpEntity.Empty)
 //    .andThen{case _ => sys.terminate()}

  val numbers = Source.fromIterator(() =>
    Iterator.continually(Random.nextInt()))
    .map(n => ByteString(s"$n\n"))
  //make conform to withoutSizeLimit constrain
  val source = limitableByteSource(numbers)

  val bytes = HttpEntity(
    ContentTypes.`application/octet-stream`,
    // transform each number to a chunk of bytes
    source.take(10000)
  )
  val postRandom = HttpRequest(HttpMethods.POST,uri = s"http://localhost:8011/random")
  runService(postRandom,bytes)
    .andThen{case _ => sys.terminate()}


}