akka/akka-http

Receiving overlarge request should not log error EntityStreamSizeException

开放

#2,125 创建于 2018年7月25日

 (0 条评论) (1 个反应) (0 位负责人)Scala (598 个派生)batch import
1 - triagedhelp wantedt:server

仓库指标

星标
 (1,311 个星标)
PR 合并指标
 (平均合并 1天 10小时) (30 天内合并 2 个 PR)

描述

Hi,

I want to prevent the user from uploading huge files. In my opinion the user should neither be able to blow up the system nor make it write errors to the log. Using akka.http.[server|client].parsing.max-content-length or MiscDirectives#withSizeLimit works fine but errors are logged because of: https://github.com/akka/akka/issues/25386 Example:

  private val sizeExceptionHandler = ExceptionHandler {
    case _: EntityStreamSizeException =>
      complete(StatusCodes.BadRequest)
  }

  val upload: Route = post {
    handleExceptions(sizeExceptionHandler) {
      withSizeLimit(2000000) {
        fileUpload("file") {
          case (_, _) =>
            //Do something
            complete(StatusCodes.OK)
        }
      }
    }
  }

To not get an error logged and still prevent the user from blow up the system I needed to implement it in a very ugly way not using akka http MiscDirectives#withSizeLimit function .

  val upload: Route = post {
    withoutSizeLimit {
      fileUpload("file") {
        case (fileInfo, source) =>
          onComplete(
            source.runFold(Array[Byte]())((totalBytes, bytes) => {
              val newTotal = totalBytes ++ bytes
              if (newTotal.length > 2000000) throw new IllegalStateException(s"File size exceed limit of 2000000 byte")
              newTotal
            }).map({
              //Do something
              bytes => println(bytes)
            }).recover {
              case e: IllegalStateException =>
                HttpResponse(StatusCodes.BadRequest, entity = e.getMessage)
            })(_ => complete(s"${fileInfo.fileName} successfully uploaded"))
      }
    }
  }

Would it be possible to have MiscDirectives#withSizeLimit without having errors logged or is there a nicer working solution using other akka http functions for this?

贡献者指南