|
| 1 | +package mozilla.telemetry.glean |
| 2 | + |
| 3 | +/* This Source Code Form is subject to the terms of the Mozilla Public |
| 4 | + * License, v. 2.0. If a copy of the MPL was not distributed with this |
| 5 | + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
| 6 | + |
| 7 | +// THIS IS A CLONE OF THE OTHER HTTPCONFIG USED FOR APPLICATION SERVICES |
| 8 | +// SEEMED CONVIENIENT FOR THE SAKE OF THE PROTOTYPE, PREFERRABLY |
| 9 | +// WE CAN USE THE SAME HTTPCONFIG, BUT I COULDN'T WRAP MY HEAD ON A QUICK |
| 10 | +// WAY TO DO THAT FOR A PROTOTYPE |
| 11 | + |
| 12 | +import com.google.protobuf.ByteString |
| 13 | +import mozilla.components.concept.fetch.Client |
| 14 | +import mozilla.components.concept.fetch.MutableHeaders |
| 15 | +import mozilla.components.concept.fetch.Request |
| 16 | +import mozilla.telemetry.glean.rust.LibGleanFFI |
| 17 | +import mozilla.telemetry.glean.rust.RawFetchCallback |
| 18 | +import mozilla.telemetry.glean.rust.RustBuffer |
| 19 | +import java.util.concurrent.TimeUnit |
| 20 | +import java.util.concurrent.locks.ReentrantReadWriteLock |
| 21 | +import kotlin.concurrent.read |
| 22 | +import kotlin.concurrent.write |
| 23 | + |
| 24 | +/** |
| 25 | + * Singleton allowing management of the HTTP backend |
| 26 | + * used by Rust components. |
| 27 | + */ |
| 28 | +object RustHttpConfig { |
| 29 | + // Protects imp/client |
| 30 | + private var lock = ReentrantReadWriteLock() |
| 31 | + @Volatile |
| 32 | + private var client: Lazy<Client>? = null |
| 33 | + // Important note to future maintainers: if you mess around with |
| 34 | + // this code, you have to make sure `imp` can't get GCed. Extremely |
| 35 | + // bad things will happen if it does! |
| 36 | + @Volatile |
| 37 | + private var imp: CallbackImpl? = null |
| 38 | + |
| 39 | + /** |
| 40 | + * Set the HTTP client to be used by all Rust code. |
| 41 | + * the `Lazy`'s value is not read until the first request is made. |
| 42 | + */ |
| 43 | + @Synchronized |
| 44 | + fun setClient(c: Lazy<Client>) { |
| 45 | + lock.write { |
| 46 | + client = c |
| 47 | + if (imp == null) { |
| 48 | + imp = CallbackImpl() |
| 49 | + LibGleanFFI.INSTANCE.viaduct_initialize(imp!!) |
| 50 | + } |
| 51 | + } |
| 52 | + } |
| 53 | + |
| 54 | + internal fun convertRequest(request: MsgTypes.Request): Request { |
| 55 | + val headers = MutableHeaders() |
| 56 | + for (h in request.headersMap) { |
| 57 | + headers.append(h.key, h.value) |
| 58 | + } |
| 59 | + return Request( |
| 60 | + url = request.url, |
| 61 | + method = convertMethod(request.method), |
| 62 | + headers = headers, |
| 63 | + connectTimeout = Pair(request.connectTimeoutSecs.toLong(), TimeUnit.SECONDS), |
| 64 | + readTimeout = Pair(request.readTimeoutSecs.toLong(), TimeUnit.SECONDS), |
| 65 | + body = if (request.hasBody()) { |
| 66 | + Request.Body(request.body.newInput()) |
| 67 | + } else { |
| 68 | + null |
| 69 | + }, |
| 70 | + redirect = if (request.followRedirects) { |
| 71 | + Request.Redirect.FOLLOW |
| 72 | + } else { |
| 73 | + Request.Redirect.MANUAL |
| 74 | + }, |
| 75 | + cookiePolicy = Request.CookiePolicy.OMIT, |
| 76 | + useCaches = request.useCaches |
| 77 | + ) |
| 78 | + } |
| 79 | + |
| 80 | + @Suppress("TooGenericExceptionCaught", "ReturnCount") |
| 81 | + internal fun doFetch(b: RustBuffer.ByValue): RustBuffer.ByValue { |
| 82 | + lock.read { |
| 83 | + try { |
| 84 | + val request = MsgTypes.Request.parseFrom(b.asCodedInputStream()) |
| 85 | + val rb = try { |
| 86 | + // Note: `client!!` is fine here, since if client is null, |
| 87 | + // we wouldn't have yet initialized |
| 88 | + val resp = client!!.value.fetch(convertRequest(request)) |
| 89 | + val rb = MsgTypes.Response.newBuilder() |
| 90 | + .setUrl(resp.url) |
| 91 | + .setStatus(resp.status) |
| 92 | + .setBody(resp.body.useStream { |
| 93 | + ByteString.readFrom(it) |
| 94 | + }) |
| 95 | + |
| 96 | + for (h in resp.headers) { |
| 97 | + rb.putHeaders(h.name, h.value) |
| 98 | + } |
| 99 | + rb |
| 100 | + } catch (e: Throwable) { |
| 101 | + MsgTypes.Response.newBuilder().setExceptionMessage("fetch error: ${e.message ?: e.javaClass.canonicalName}") |
| 102 | + } |
| 103 | + val built = rb.build() |
| 104 | + val needed = built.serializedSize |
| 105 | + val outputBuf = LibGleanFFI.INSTANCE.viaduct_alloc_bytebuffer(needed) |
| 106 | + try { |
| 107 | + // This is only null if we passed a negative number or something to |
| 108 | + // viaduct_alloc_bytebuffer. |
| 109 | + val stream = outputBuf.asCodedOutputStream()!! |
| 110 | + built.writeTo(stream) |
| 111 | + return outputBuf |
| 112 | + } catch (e: Throwable) { |
| 113 | + // Note: we want to clean this up only if we are not returning it to rust. |
| 114 | + LibGleanFFI.INSTANCE.viaduct_destroy_bytebuffer(outputBuf) |
| 115 | + LibGleanFFI.INSTANCE.viaduct_log_error("Failed to write buffer: ${e.message}") |
| 116 | + throw e |
| 117 | + } |
| 118 | + } finally { |
| 119 | + LibGleanFFI.INSTANCE.viaduct_destroy_bytebuffer(b) |
| 120 | + } |
| 121 | + } |
| 122 | + } |
| 123 | +} |
| 124 | + |
| 125 | +internal fun convertMethod(m: MsgTypes.Request.Method): Request.Method { |
| 126 | + return when (m) { |
| 127 | + MsgTypes.Request.Method.GET -> Request.Method.GET |
| 128 | + MsgTypes.Request.Method.POST -> Request.Method.POST |
| 129 | + MsgTypes.Request.Method.HEAD -> Request.Method.HEAD |
| 130 | + MsgTypes.Request.Method.OPTIONS -> Request.Method.OPTIONS |
| 131 | + MsgTypes.Request.Method.DELETE -> Request.Method.DELETE |
| 132 | + MsgTypes.Request.Method.PUT -> Request.Method.PUT |
| 133 | + MsgTypes.Request.Method.TRACE -> Request.Method.TRACE |
| 134 | + MsgTypes.Request.Method.CONNECT -> Request.Method.CONNECT |
| 135 | + } |
| 136 | +} |
| 137 | + |
| 138 | +internal class CallbackImpl : RawFetchCallback { |
| 139 | + @Suppress("TooGenericExceptionCaught") |
| 140 | + override fun invoke(b: RustBuffer.ByValue): RustBuffer.ByValue { |
| 141 | + try { |
| 142 | + return RustHttpConfig.doFetch(b) |
| 143 | + } catch (e: Throwable) { |
| 144 | + LibGleanFFI.INSTANCE.viaduct_log_error("doFetch failed: ${e.message}") |
| 145 | + // This is our last resort. It's bad news should we fail to |
| 146 | + // return something from this function. |
| 147 | + return RustBuffer.ByValue() |
| 148 | + } |
| 149 | + } |
| 150 | +} |
0 commit comments