jpsingleton / Huxley2

A cross-platform JSON proxy for the GB railway Live Departure Boards SOAP API
https://huxley2.azurewebsites.net
European Union Public License 1.2
51 stars 74 forks source link

Getting service details consistently fails #17

Closed dylanmaryk closed 1 year ago

dylanmaryk commented 1 year ago

I don't know if this might be something on National Rail's side, maybe something temporary, or something due to the recent service ID changes, but for any service ID I always get a crash here:

https://github.com/jpsingleton/Huxley2/blob/cc732c68fc5acc07f7f0190ffcaae75332c730ee/Huxley2/Services/ServiceDetailsService.cs#L119

Here is the full stack trace:

System.ServiceModel.FaultException:
   at System.ServiceModel.Channels.ServiceChannel.HandleReply (System.Private.ServiceModel, Version=4.7.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a)
   at System.ServiceModel.Channels.ServiceChannel.EndCall (System.Private.ServiceModel, Version=4.7.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a)
   at System.ServiceModel.Channels.ServiceChannelProxy+TaskCreator+<>c__DisplayClass1_0.<CreateGenericTask>b__0 (System.Private.ServiceModel, Version=4.7.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a)
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw (System.Private.CoreLib, Version=6.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e)
   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess (System.Private.CoreLib, Version=6.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e)
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification (System.Private.CoreLib, Version=6.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e)
   at Huxley2.Services.ServiceDetailsService+<GetServiceDetailsAsync>d__5.MoveNext (Huxley2, Version=2.0.3.0, Culture=neutral, PublicKeyToken=null: /home/vsts/work/1/s/Huxley2/Services/ServiceDetailsService.cs:119)
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw (System.Private.CoreLib, Version=6.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e)
   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess (System.Private.CoreLib, Version=6.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e)
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification (System.Private.CoreLib, Version=6.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e)
   at System.Runtime.CompilerServices.TaskAwaiter`1.GetResult (System.Private.CoreLib, Version=6.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e)
   at Huxley2.Controllers.ServiceController+<Get>d__3.MoveNext (Huxley2, Version=2.0.3.0, Culture=neutral, PublicKeyToken=null: /home/vsts/work/1/s/Huxley2/Controllers/ServiceController.cs:52)

National Rail always return an error 500. Some of the additional details I get in Azure:

Screenshot 2022-12-30 at 17 29 57
dylanmaryk commented 1 year ago

@nathancartlidge would appreciate your input if you're available 🙏

nathancartlidge commented 1 year ago

I can't immediately reproduce this:

dylanmaryk commented 1 year ago

@nathancartlidge here's one example: https://huxley2.azurewebsites.net/service/NDY5MTQ4NkNMUEhNSk1f

I'm also using the latest commits on my own fork that I use for my own Azure instance.

dylanmaryk commented 1 year ago

@nathancartlidge ah, is it maybe that serviceIdUrlSafe is not using the new format?

dylanmaryk commented 1 year ago

@nathancartlidge serviceIdPercentEncoded and serviceIdGuid work fine for me so I think that's the issue

nathancartlidge commented 1 year ago

yep - that one doesn't work for me either, looks like a bug I introduced in the last commit unfortunately

This patch should fix it in the short term:

@@ -95,25 +95,25 @@ namespace Huxley2.Services
             // Encoder available as part of ASP.NET Core: Microsoft.Extensions.WebEncoders
             // For more info read ASP.NET Core 2 High Performance (https://unop.uk/book) :)

-            if (request.ServiceId.Length == 22)
+            if (request.ServiceId.Length == 20)
             {
                 var sidBytes = WebEncoders.Base64UrlDecode(request.ServiceId);
-                if (sidBytes.Length == 16)
+                if (sidBytes.Length == 15)
                 {
-                    request.ServiceId = Convert.ToBase64String(sidBytes);
+                    request.ServiceId = System.Text.Encoding.UTF8.GetString(sidBytes);
                 }
             }

-            // If ID wasn't percent-encoded then it may be missing / + =
-            // We try to fix it up if it isn't the correct length
-            while (!request.ServiceId.EndsWith("==", StringComparison.OrdinalIgnoreCase))
-            {
-                request.ServiceId += "=";
-            }
-            while (request.ServiceId.Length < 24)
-            {
-                request.ServiceId = "/" + request.ServiceId;
-            }
dylanmaryk commented 1 year ago

@nathancartlidge thanks! I can just use serviceIdGuid instead, which seems to always be URL-safe already now? Feel free to submit a PR with the fix whenever you have time, but I can just use the alternative so I don't need it in a hurry.

nathancartlidge commented 1 year ago

all three other options (serviceId, serviceIdPercentEncoded, and serviceIdGuid) should now be url-safe, so feel free to choose any of those!

I'll hopefully be able to get a commit out tonight to address this and the improvements from #14