代理请求可以播放到其他服务

时间:2016-08-24 13:05:23

标签: scala playframework

我想用不同的内容类型代理所有类型的请求。我想出了:

  private def proxy(path: String, req: Request[_]) = {
    proxyRequest(path, req).map { data =>
      wsResponseToPlayResponse(data)
    }
  }

  def proxyRequest(path: String, req: Request[_]): Future[WSResponse] = {
    Logger.debug(s"Method: ${req.method}")
    val request = ws.url(path)
      .withMethod(req.method)
      .withHeaders(req.headers.headers: _*)
      .withBody(req.body.toString)
    request.execute()
  }

  private def wsResponseToPlayResponse(data: WSResponse): Result = {
    val transformHeaders = data.allHeaders.map {
      case (key, values) => (key, values.reduce(_ + _))
    }
    Result(
      ResponseHeader(data.status, transformHeaders),
      Enumerator(data.bodyAsBytes)
    )
  }

  private def genLink(service: String, host: String, port: String, path: String) = path match {
    case "" => s"$host:$port/api/$service"
    case knownPath => s"$host:$port/api/$service/$path"
  }

但它不适用于图像。所以试着这样做:

  def postImage(ref:String) = Action.async(parse.raw) { req =>
    ws.url(s"$BASE_URL/api/images/$ref").withHeaders(req.headers.headers:_*)
      .post(req.body.asFile)
      .map(wsResponseToPlayResponse)
  }

但这是错误的文件,因为外部服务正在报告:

images_1         | 10:41:36.608 [New I/O worker #4] DEBUG c.n.h.c.p.netty.handler.HttpProtocol - 
images_1         | 
images_1         | Request DefaultHttpRequest(chunked: false)
images_1         | POST /v1_1/smoffice/image/upload HTTP/1.1
images_1         | Content-Length: 62234
images_1         | Content-Type: multipart/form-data; boundary=Fu-UTwfLaSq-uMupRJ23ln6qi4K-KMrj
images_1         | Connection: keep-alive
images_1         | Host: api.cloudinary.com
images_1         | Accept: */*
images_1         | User-Agent: cld-scala-0.9.9-SNAPSHOT
images_1         | 
images_1         | Response DefaultHttpResponse(chunked: false)
images_1         | HTTP/1.1 400 Bad Request
images_1         | Cache-Control: no-cache
images_1         | Content-Type: application/json; charset=utf-8
images_1         | Date: Wed, 24 Aug 2016 10:41:36 GMT
images_1         | Server: cloudinary
images_1         | Status: 400 Bad Request
images_1         | X-Cld-Error: Invalid image file
images_1         | X-Request-Id: 93407c1fab141e2c
images_1         | X-UA-Compatible: IE=Edge,chrome=1
images_1         | Content-Length: 42
images_1         | Connection: keep-alive
images_1         | 
images_1         | 10:41:36.609 [New I/O worker #4] DEBUG c.n.h.c.p.n.channel.ChannelManager - Adding key: https://api.cloudinary.com:443 for channel [id: 0x58dbfb16, /172.19.0.6:51752 => api.cloudinary.com/52.20.33.251:443]
images_1         | 10:41:36.612 [images-akka.actor.default-dispatcher-460] ERROR akka.actor.ActorSystemImpl - Error during processing of request HttpRequest(HttpMethod(POST),http://red-services.isaacloud.com/api/images/event,List(Host: red-services.isaacloud.com, X-Forwarded-For: 91.227.215.2, 172.19.0.8, Connection: close, Accept: application/json, text/plain, */*, User-Agent: Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Ubuntu Chromium/51.0.2704.79 Chrome/51.0.2704.79 Safari/537.36, Accept-Language: pl-PL, pl;q=0.8, en-US;q=0.6, en;q=0.4, Cookie: admin=%22tajne%22; PLAY_SESSION=19bbf7a2603d4bbff9ebb4620a058dbc4014ec73-login=%7B%22id%22%3A43%2C%22firstName%22%3A%22soi%22%2C%22lastName%22%3A%22admin%22%2C%22email%22%3A%22soadmin%40soi.soi%22%7D; SESSION_ID=43, Origin: http://localhost:9001, Referer: http://localhost:9001/, Accept-Encoding: utf-8, Timeout-Access: <function1>),HttpEntity.Strict(multipart/form-data; boundary=----WebKitFormBoundaryZXKxaJB7V7ND8hU5; charset=UTF-8,------WebKitFormBoundaryZXKxaJB7V7ND8hU5
images_1         | Content-Disposition: form-data; name="file"; filename="7.png"
images_1         | Content-Type: image/png
images_1         | 
images_1         | �PNG
images_1         | 
IHDR�ï¿ï¿½ï¿½>0|      pHYs.#.#x�?v
images_1         | OiCCPPhotoshop ICC profilexÚSgTS�=���BK���Ko RB���&*!   J�!��Q�EEȠ�����,
                                                                                                                                   �
images_1         | ��!���������{�kּ������>�������
                                                                                                   �H3Q5�
                                                                                                             �B�������.@�
images_1         | $�d!s�#�~<<+"��x�
                                               �M��0���B�\���t�8�@z�B�@F���&S�`�cb�P-`'������{[�!�� e�Dh;��V�EX0fK�9�-0IWfH����
             �
               0Q��){`�##x��F�W<�+��*x��<�$9E�-qWW.(�I+6aa�@.�y�2�4���������x����6��_-���"bb���ϫp@�t~��,/��;�m��%�h^
                     �u��f�@����W�p�~<<E���������J�B[a�W}�g�_�W�l�~<������$�2]�G�����Lï¿½Ï   �b���G��
    ���"�Ib�X*�Qq�D���2�"�B�)�%��d��,�>�5�j>{�-�]c�K'Xt�����o���h���w��?�G�%�fI�q^D$.Tʳ?ï¿D��*�A������
           �`6�B$��BB
images_1         | d�r`)��B(�Ͱ*`/�@4�Qh��p.�U�=p���(��   Aï¿a!Úb�X#ï¿ï¿½ï¿½ï¿½!�H�$ ÉQ"K�5H1R�T UH�=r9�\F��;�2����G1���Q=�
                                                                                                                                                                                               �C��7�F�
                 �dt1�����r�=�6��ЫhÚ>C�0��3�l0.��B�8,   �c˱"�
                                                                                                          ���V����cϱw�E� 6wB aAHXLXN�H� $4�    7   �Q�'"��K�&���b21�XH,#��/{�C�7$�C2'��I��T��F�nR#�,��4H#���dk�9�, +È�����3���[
images_1         | �b@q��S�(R�jJ��4�e�2AU��Rݨ�T5�ZB���R�Q��4u�9ÍIK�����hh�i��t�ÝN��W���G����w
��Çg(�gw��L�Ó�T071�����oUX*�*|��
images_1         | �J�&�T����ު
                                             U�U�T��^S}�FU3S��    �U��P�Sg�;���g�oT?�~Y��Y�L�OC�Q��_��� 
��u�5�&���|v*�����=���9C3J3W�R��f?��q��tN   �(���~���)�)�L�1e\k����X�H�Q�G��6������E�Y��A�J'\'Gg����S�Sݧ
images_1         | �M= ... (61476 bytes total)),HttpProtocol(HTTP/1.0))

这可能很难重现。有谁知道如何制作通用代理功能?

0 个答案:

没有答案