Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Broken pipe #624

Open
zhuyanhua1 opened this issue Mar 28, 2022 · 2 comments
Open

Broken pipe #624

zhuyanhua1 opened this issue Mar 28, 2022 · 2 comments

Comments

@zhuyanhua1
Copy link

No description provided.

@rufan-nativesol
Copy link

java.net.SocketException: Broken pipe at java.net.SocketOutputStream.socketWrite0(Native Method) at java.net.SocketOutputStream.socketWrite(SocketOutputStream.java:116) at java.net.SocketOutputStream.write(SocketOutputStream.java:156) at fi.iki.elonen.NanoHTTPD$Response.sendBody(NanoHTTPD.java:1694) at fi.iki.elonen.NanoHTTPD$Response.sendBodyWithCorrectEncoding(NanoHTTPD.java:1667) at fi.iki.elonen.NanoHTTPD$Response.sendBodyWithCorrectTransferAndEncoding(NanoHTTPD.java:1657) at fi.iki.elonen.NanoHTTPD$Response.send(NanoHTTPD.java:1624) at fi.iki.elonen.NanoHTTPD$HTTPSession.execute(NanoHTTPD.java:957) at fi.iki.elonen.NanoHTTPD$ClientHandler.run(NanoHTTPD.java:192) at java.lang.Thread.run(Thread.java:1012)

@rufan-nativesol
Copy link

`override fun serve(session: IHTTPSession): Response {
val uri = session.uri
Log.d(TAG, "serve: $uri")
val file = File(uri.substring(1)) // Remove leading '/' from URI to get file path

    if (!file.exists() || !file.isFile) {
        return newFixedLengthResponse(Response.Status.NOT_FOUND, "text/plain", "File not found")
    }

    val etag = Integer.toHexString(file.hashCode())
    val range = session.headers["range"]
    return if (range == null) {
        // Regular request, send the entire file
        val mimeType = file.getMimeType()
        val inputStream = FileInputStream(file)
        newFixedLengthResponse(Response.Status.OK, mimeType, inputStream, file.length()).apply {
            addHeader("ETag", etag)
        }
    } else {
        // Range request, parse the range and send the requested part of the file
        val ranges = range.split("=")[1].split("-")
        val start = ranges[0].toLong()
        val end = if (ranges.size > 1 && ranges[1].isNotEmpty()) {
            ranges[1].toLong()
        } else {
            file.length() - 1
        }
        val length = end - start + 1

        if (start >= file.length() || end >= file.length()) {
            return newFixedLengthResponse(
                Response.Status.RANGE_NOT_SATISFIABLE,
                "text/plain",
                ""
            )
                .apply {
                    addHeader("Content-Range", "bytes */${file.length()}")
                }
        }

        val inputStream = FileInputStream(file)
        inputStream.skip(start)

        val response = newFixedLengthResponse(
            Response.Status.PARTIAL_CONTENT,
            file.getMimeType(),
            inputStream,
            length
        )
        response.addHeader("Content-Range", "bytes $start-$end/${file.length()}")
        response.addHeader("ETag", etag)
        response.addHeader("Accept-Ranges", "bytes")
        response
    }
}

`

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants