-
-
Notifications
You must be signed in to change notification settings - Fork 86
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix compression and tests therefore (#108)
* fix compression and tests therefore * Comply with recommended changes * Revert some formatting changes and rename variable * Move echo server to a separate file * Remove unused import * Fix Mima * Fix compilation error in Scala 3 Co-authored-by: Tom Ballard <tomballard@redangus.org> Co-authored-by: Lorenzo Gabriele <lorenzolespaul@gmail.com>
- Loading branch information
1 parent
11a9787
commit 7e18f64
Showing
6 changed files
with
155 additions
and
41 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
package requests | ||
|
||
import com.sun.net.httpserver.{HttpExchange, HttpHandler, HttpServer} | ||
import java.io._ | ||
import java.net.InetSocketAddress | ||
import java.util.zip.{GZIPInputStream, InflaterInputStream} | ||
import requests.Compress._ | ||
import scala.annotation.tailrec | ||
import scala.collection.mutable.StringBuilder | ||
|
||
object ServerUtils { | ||
def usingEchoServer(f: Int => Unit): Unit = { | ||
val server = new EchoServer | ||
try f(server.getPort()) | ||
finally server.stop() | ||
} | ||
|
||
private class EchoServer extends HttpHandler { | ||
private val server: HttpServer = HttpServer.create(new InetSocketAddress(0), 0) | ||
server.createContext("/echo", this) | ||
server.setExecutor(null); // default executor | ||
server.start() | ||
|
||
def getPort(): Int = server.getAddress.getPort | ||
|
||
def stop(): Unit = server.stop(0) | ||
|
||
override def handle(t: HttpExchange): Unit = { | ||
val h: java.util.List[String] = | ||
t.getRequestHeaders.get("Content-encoding") | ||
val c: Compress = | ||
if (h == null) None | ||
else if (h.contains("gzip")) Gzip | ||
else if (h.contains("deflate")) Deflate | ||
else None | ||
val msg = new Plumper(c).decompress(t.getRequestBody) | ||
t.sendResponseHeaders(200, msg.length) | ||
t.getResponseBody.write(msg.getBytes()) | ||
} | ||
} | ||
|
||
/** Stream uncompresser | ||
* @param c | ||
* Compression mode | ||
*/ | ||
private class Plumper(c: Compress) { | ||
|
||
private def wrap(is: InputStream): InputStream = | ||
c match { | ||
case None => is | ||
case Gzip => new GZIPInputStream(is) | ||
case Deflate => new InflaterInputStream(is) | ||
} | ||
|
||
def decompress(compressed: InputStream): String = { | ||
val gis = wrap(compressed) | ||
val br = new BufferedReader(new InputStreamReader(gis, "UTF-8")) | ||
val sb = new StringBuilder() | ||
|
||
@tailrec | ||
def read(): Unit = { | ||
val line = br.readLine | ||
if (line != null) { | ||
sb.append(line) | ||
read() | ||
} | ||
} | ||
|
||
read() | ||
br.close() | ||
gis.close() | ||
compressed.close() | ||
sb.toString() | ||
} | ||
} | ||
|
||
} |