Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 1 addition & 10 deletions Package.resolved

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ let package = Package(
targets: [
.target(
name: "GutenbergKit",
dependencies: ["SwiftSoup", "SVGView", "GutenbergKitResources"],
dependencies: ["SwiftSoup", "SVGView", "GutenbergKitResources", "GutenbergKitHTTP"],
path: "ios/Sources/GutenbergKit",
exclude: ["Gutenberg"],
packageAccess: false
Expand Down
1 change: 1 addition & 0 deletions android/Gutenberg/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ dependencies {
implementation(libs.jsoup)
implementation(libs.okhttp)

testImplementation(libs.json)
testImplementation(libs.junit)
testImplementation(kotlin("test"))
testImplementation(libs.kotlinx.coroutines.test)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,18 @@ class InstrumentedFixtureTests {

try {
parser.parseRequest()
fail("$description: expected error $expectedError but parsing succeeded")
// Non-fatal errors (e.g., payloadTooLarge) are exposed via
// pendingParseError instead of being thrown.
val pendingError = parser.pendingParseError
if (pendingError != null) {
assertEquals(
expectedError,
pendingError.errorId,
"$description: expected $expectedError but got ${pendingError.errorId}"
)
} else {
fail("$description: expected error $expectedError but parsing succeeded")
}
} catch (e: HTTPRequestParseException) {
assertEquals(
expectedError,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,25 @@ class GutenbergView : FrameLayout {

var requestInterceptor: GutenbergRequestInterceptor = DefaultGutenbergRequestInterceptor()

/** Optional delegate for customizing media upload behavior (resize, transcode, custom upload). */
var mediaUploadDelegate: MediaUploadDelegate? = null
set(value) {
if (field === value) return
field = value
// Stop any previously running server before starting a new one.
uploadServer?.stop()
uploadServer = null
// (Re)start the upload server so it captures the delegate.
// This handles the common case where the delegate is set after
// construction but before the editor finishes loading.
if (value != null) {
startUploadServer()
}
}

private var uploadServer: MediaUploadServer? = null
private val uploadHttpClient: okhttp3.OkHttpClient by lazy { okhttp3.OkHttpClient() }

private var onFileChooserRequested: ((Intent, Int) -> Unit)? = null
private var contentChangeListener: ContentChangeListener? = null
private var historyChangeListener: HistoryChangeListener? = null
Expand Down Expand Up @@ -571,7 +590,12 @@ class GutenbergView : FrameLayout {
}

private fun setGlobalJavaScriptVariables() {
val gbKit = GBKitGlobal.fromConfiguration(configuration, dependencies)
val gbKit = GBKitGlobal.fromConfiguration(
configuration,
dependencies,
nativeUploadPort = uploadServer?.port,
nativeUploadToken = uploadServer?.token
)
val gbKitJson = gbKit.toJsonString()
val gbKitConfig = """
window.GBKit = $gbKitJson;
Expand All @@ -582,6 +606,26 @@ class GutenbergView : FrameLayout {
}


private fun startUploadServer() {
if (configuration.siteApiRoot.isEmpty() || configuration.authHeader.isEmpty()) return

try {
val defaultUploader = DefaultMediaUploader(
httpClient = uploadHttpClient,
siteApiRoot = configuration.siteApiRoot,
authHeader = configuration.authHeader,
siteApiNamespace = configuration.siteApiNamespace.toList()
)
uploadServer = MediaUploadServer(
uploadDelegate = mediaUploadDelegate,
defaultUploader = defaultUploader,
cacheDir = context.cacheDir
)
} catch (e: Exception) {
Log.w(TAG, "Failed to start upload server", e)
}
}

fun clearConfig() {
val jsCode = """
delete window.GBKit;
Expand Down Expand Up @@ -1003,6 +1047,8 @@ class GutenbergView : FrameLayout {
override fun onDetachedFromWindow() {
super.onDetachedFromWindow()
stopNetworkMonitoring()
uploadServer?.stop()
uploadServer = null
clearConfig()
// Cancel in-flight animations to prevent withEndAction callbacks from
// firing on detached views.
Expand Down Expand Up @@ -1075,6 +1121,8 @@ class GutenbergView : FrameLayout {
}

companion object {
private const val TAG = "GutenbergView"

/** Hosts that are safe to serve assets over HTTP (local development only). */
private val LOCAL_HOSTS = setOf("localhost", "127.0.0.1", "10.0.2.2")

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,11 @@ data class HttpRequest(
val target: String,
val headers: Map<String, String>,
val body: org.wordpress.gutenberg.http.RequestBody? = null,
val parseDurationMs: Double = 0.0
val parseDurationMs: Double = 0.0,
/** A server-detected error that occurred after headers were parsed
* (e.g., payload too large). When set, the handler is responsible
* for building an appropriate error response. */
val serverError: org.wordpress.gutenberg.http.HTTPRequestParseError? = null
) {
/**
* Returns the value of the first header matching the given name (case-insensitive).
Expand Down Expand Up @@ -277,13 +281,12 @@ class HttpServer(
val buffer = ByteArray(READ_CHUNK_SIZE)

// Phase 1: receive headers only.
while (!parser.state.hasHeaders) {
if (System.nanoTime() > deadlineNanos) {
throw SocketTimeoutException("Read deadline exceeded")
}
val bytesRead = input.read(buffer)
if (bytesRead == -1) break
parser.append(buffer.copyOfRange(0, bytesRead))
readUntil(parser, input, buffer, deadlineNanos) { it.hasHeaders }

// Drain oversized body before throwing so the
// client receives the 413 (RFC 9110 §15.5.14).
if (parser.state == HTTPRequestParser.State.DRAINING) {
readUntil(parser, input, buffer, deadlineNanos) { it.isComplete }
}

// Validate headers (triggers full RFC validation).
Expand Down Expand Up @@ -312,6 +315,31 @@ class HttpServer(
return
}

// If the parser detected a non-fatal error (e.g., payload too
// large after drain), let the handler build the response.
parser.pendingParseError?.let { error ->
val parseDurationMs = (System.nanoTime() - parseStart) / 1_000_000.0
val request = HttpRequest(
method = partial.method,
target = partial.target,
headers = partial.headers,
parseDurationMs = parseDurationMs,
serverError = error
)
val response = try {
handler(request)
} catch (e: Exception) {
Log.e(TAG, "Handler threw", e)
HttpResponse(
status = error.httpStatus,
body = (STATUS_TEXT[error.httpStatus] ?: "Error").toByteArray()
)
}
sendResponse(socket, response)
Log.d(TAG, "${partial.method} ${partial.target} → ${response.status} (${"%.1f".format(parseDurationMs)}ms)")
return
}

// Check auth before consuming body to avoid buffering up to
// maxBodySize for unauthenticated clients.
// OPTIONS is exempt because CORS preflight requests
Expand Down Expand Up @@ -341,14 +369,7 @@ class HttpServer(
}

// Phase 2: receive body (skipped if already complete).
while (!parser.state.isComplete) {
if (System.nanoTime() > deadlineNanos) {
throw SocketTimeoutException("Read deadline exceeded")
}
val bytesRead = input.read(buffer)
if (bytesRead == -1) break
parser.append(buffer.copyOfRange(0, bytesRead))
}
readUntil(parser, input, buffer, deadlineNanos) { it.isComplete }

// Final parse with body.
val parsed = try {
Expand Down Expand Up @@ -406,6 +427,24 @@ class HttpServer(
}
}

/** Reads data into the parser until [condition] is satisfied or the connection closes. */
private fun readUntil(
parser: HTTPRequestParser,
input: BufferedInputStream,
buffer: ByteArray,
deadlineNanos: Long,
condition: (HTTPRequestParser.State) -> Boolean
) {
while (!condition(parser.state)) {
if (System.nanoTime() > deadlineNanos) {
throw SocketTimeoutException("Read deadline exceeded")
}
val bytesRead = input.read(buffer)
if (bytesRead == -1) break
parser.append(buffer.copyOfRange(0, bytesRead))
}
}

private fun sendResponse(socket: Socket, response: HttpResponse) {
val output = socket.getOutputStream()
output.write(serializeResponse(response))
Expand Down
Loading
Loading