Troubleshooting process of uploading video to Cloudflare using Java’s TusClient

Original link: http://afoo.me/posts/2022-12-06-tus-stuck-in-cloudflare-stream.html

cf-stream-upload-ui.jpg

Troubleshooting process of uploading video to Cloudflare using Java’s TusClient – ​​Mr. Fuqiang said: the thinking and precipitation of an architect

Troubleshooting process of uploading video to Cloudflare using Java’s TusClient

Wang Fuqiang

2022-12-06


I originally wanted to write a simple tool to upload videos to Cloudflare Stream, but a few hours have passed…

Debug is a fucking time-killer.

The story begins with the returned 400 status code:

unexpected status code (400) while uploading chunk

Of course, the above information comes from the exception thrown by Java’s TusClient, and it is actually caught in the code. If the status code is 400, it will definitely throw more than this.

The 400 status code means Bad request , which means there is a problem with the upload request I submitted? It stands to reason that I wrote it according to your official Cloudflare Stream documentation, so there should be no problem, so let’s Google it.

I found that most of the posts found in the cloudflare forum are useless, although they are all appearances of 400 status codes.

The first thing to check is that the authentication headers are all right. After that, I will debug one header and one header! Refer to the API code in other languages ​​of cloudflare to experiment with various parameters, but to no avail…

Search again, and rummage through the search results (yes, even ChatGPT can’t help you with this job), and found this: https://github.com/tus/tus-java-client/issues/66 , tnnd, it turns out It is cloudflare’s server-side incompatibility with tus that causes 400, day! In other words, the implementation of tusclient has a fallback and does not follow the most basic interaction protocol, because some JDK versions of http connection do not support the PATCH method. (How important is the underlying library! Defects seriously affect a large area. This also shows that no one person or organization can do everything well. Isn’t it good to open up and let different people and organizations do what they are good at?)

How to do? I just want to use Java, and I don’t want to write a Java Client for Tus myself (otherwise, wouldn’t it just go in? I originally wanted to write a simple upload client), I can only seek “black” magic, and then Got me to this: https://stackoverflow.com/questions/25163131/httpurlconnection-invalid-http-method-patch

Of course, it is still not ready-made. The old Java reflection code has obviously been blocked under the JDK of Java19. Therefore, old dogs like us have to use new skills to do old things. It is nasty, but it works:

 private def hackHttpURLConnectionByAppendingHttpMethodPatch (): Unit = { try { val methodsField : Field = classOf [ HttpURLConnection ]. getDeclaredField ( "methods" ) methodsField . setAccessible ( true ) val lookup = MethodHandles . privateLookupIn ( classOf [ Field ], MethodHandles . lookup ()); val MODIFIERS = lookup . findVarHandle ( classOf [ Field ], "modifiers" , classOf [ Int ]) if ( Modifier . isFinal ( methodsField . getModifiers )) { MODIFIERS . set ( methodsField , methodsField . getModifiers & ~ Modifier . FINAL ) } methodsField . set ( null /*static field*/ , Array ( "GET" , "POST" , "HEAD" , "OPTIONS" , "PUT" , "DELETE" , "TRACE" , "PATCH" )) } catch { case t : Throwable => logger . warn ( ExceptionUtils . getStackTrace ( t )) } }

Then, just call this method once at any suitable point before calling TusClient to do something.

Oh, yes, the last key point, and remember to add it after executing the java command (if running in Intellij IDE, drive to the VM parameter):

 --illegal-access=permit --add-opens java.base/java.net=ALL-UNNAMED --add-opens java.base/java.lang.reflect=ALL-UNNAMED

Well, I will share with you a long time of groping, I hope you will not use it, after all, TusClient should upgrade the HTTP implementation scheme later, the current implementation is a bit too closed, and there is no way to add an interceptor to intercept HTTP requests, so I just have to It is also drunk to be able to extract metadata from various surrounding information with regular expressions, but this is a digression;)

Anyway, feedback loop matters! Fast feedback is the basis for getting results quickly.



Subscribe to "Fu Bao Premium Subscription"


Twitter-logo.png
© Fuqiang Wang Personal Copyright, All Rights Reserved.
Copyright © Fuqiang Wang All rights reserved – Since 2004

Everything is homebrewed with pandoc and Markdown , little Scala also included.

This article is reproduced from: http://afoo.me/posts/2022-12-06-tus-stuck-in-cloudflare-stream.html
This site is only for collection, and the copyright belongs to the original author.