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

double byte strings are not handled correctly #5

Closed
top2tech opened this issue Jun 30, 2015 · 5 comments
Closed

double byte strings are not handled correctly #5

top2tech opened this issue Jun 30, 2015 · 5 comments

Comments

@top2tech
Copy link

Run the SwiftR iOS sample, for ASCII text, it works great with a browser client. But for double byte strings (e.g. Chinese characters), the browser client shows the strings correctly, but the iOS client does not handled correctly.

Repro 1:

Send from the browser client with some Chinese characters "你好“, the iOS cilent shows garbage string like this: `}

Repro 2:

Send from the browser client with some other Chinese characters "你好吗", the iOS client crashed with below error.

OnDisconnected e835293b-1df2-4b3d-b995-1d6a0d619ec3
fatal error: unexpectedly found nil while unwrapping an Optional value
(lldb) bt

  • thread Add cookie container #1: tid = 0x112228, 0x000000010021d474 libswiftCore.dylib`function signature specialization <Arg[0] = Exploded, Arg[1] = Exploded, Arg[2] = Dead, Arg[3] = Dead> of Swift._fatalErrorMessage (Swift.StaticString, Swift.StaticString, Swift.StaticString, Swift.UInt) -> () + 44, queue = 'com.apple.main-thread', stop reason = EXC_BREAKPOINT (code=1, subcode=0x10021d474)
    • frame #0: 0x000000010021d474 libswiftCore.dylib`function signature specialization <Arg[0] = Exploded, Arg[1] = Exploded, Arg[2] = Dead, Arg[3] = Dead> of Swift._fatalErrorMessage (Swift.StaticString, Swift.StaticString, Swift.StaticString, Swift.UInt) -> () + 44

      frame Add cookie container #1: 0x00000001000ed148 SwiftR`SwiftR.SignalR.shouldHandleRequest (request=0x0000000170009640, self=0x00000001700d9280)(ObjectiveC.NSURLRequest) -> Swift.Bool + 2328 at SwiftR.swift:128

      frame Converting existing Javascript SignalR to SwiftR #2: 0x00000001000ef160 SwiftR`SwiftR.SignalR.webView (webView=0x000000013f5146b0, request=0x0000000170009640, navigationType=Other, self=0x00000001700d9280)(ObjectiveC.UIWebView, shouldStartLoadWithRequest : ObjectiveC.NSURLRequest, navigationType : C.UIWebViewNavigationType) -> Swift.Bool + 120 at SwiftR.swift:180

      frame Use the main bundle by default #3: 0x00000001000ef200 SwiftR`@objc SwiftR.SignalR.webView (SwiftR.SignalR)(ObjectiveC.UIWebView, shouldStartLoadWithRequest : ObjectiveC.NSURLRequest, navigationType : C.UIWebViewNavigationType) -> Swift.Bool + 100 at SwiftR.swift:0

      frame use more performant WKWebview #4: 0x000000018aa9a0ac UIKit`-[UIWebView webView:decidePolicyForNavigationAction:request:frame:decisionListener:] + 332

      frame double byte strings are not handled correctly #5: 0x0000000185e36100 CoreFoundation__invoking___ + 144 frame #6: 0x0000000185d322fc CoreFoundation-[NSInvocation invoke] + 296
      frame On, Invoke does not return #7: 0x0000000185d36e30 CoreFoundation-[NSInvocation invokeWithTarget:] + 68 frame #8: 0x000000019559c898 WebKitLegacy-[_WebSafeForwarder forwardInvocation:] + 172
      frame How can i disconnect the connection? #9: 0x0000000185e33f6c CoreFoundation___forwarding___ + 440 frame #10: 0x0000000185d36ccc CoreFoundation_CF_forwarding_prep_0 + 92
      frame invoke server method,how get return value,eg string #11: 0x0000000185e36100 CoreFoundation**invoking_** + 144 frame #12: 0x0000000185d322fc CoreFoundation-[NSInvocation invoke] + 296
      frame Connection is automatically closed when the App enter foreground from background #13: 0x0000000194742efc WebCoreHandleDelegateSource(void*) + 120 frame #14: 0x0000000185de8240 CoreFoundationCFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION + 24
      frame Using WKWebView does not work #15: 0x0000000185de74e4 CoreFoundation__CFRunLoopDoSources0 + 264 frame #16: 0x0000000185de5594 CoreFoundation__CFRunLoopRun + 712
      frame exception in shouldHandleRequest #17: 0x0000000185d112d4 CoreFoundationCFRunLoopRunSpecific + 396 frame #18: 0x000000018f5276fc GraphicsServicesGSEventRunModal + 168
      frame How to connect to a remote server #19: 0x000000018a8d6fac UIKitUIApplicationMain + 1488 frame #20: 0x000000010003d294 SwiftR iOS Demomain + 164 at AppDelegate.swift:12
      frame critical files saved in tmp folder #21: 0x0000000197cdaa08 libdyld.dylib`start + 4

@top2tech
Copy link
Author

The crash occurred in SingalR.shouldHandleRequest
let data = s.dataUsingEncoding(NSUTF8StringEncoding, allowLossyConversion: false)!
let json: AnyObject = NSJSONSerialization.JSONObjectWithData(data, options: .allZeros, error: nil)!

the data is not null, I changed the SignalR.swift to print out the error:
(lldb) po err
Optional(Error Domain=NSCocoaErrorDomain Code=3840 "The operation couldn’t be completed. (Cocoa error 3840.)" (Unescaped control character around character 81.) UserInfo=0x17027de40 {NSDebugDescription=Unescaped control character around character 81.})

looks like NSJSONSerialization.JSONObjectWithData has some issue.

@top2tech
Copy link
Author

here is the content of data:
(lldb) po data
<7b226875 62223a22 64656d6f 68756222 2c226d65 74686f64 223a2263 6861744d 65737361 6765222c 22617267 756d656e 7473223a 7b226e61 6d65223a 22736466 64222c22 6d657373 61676522 3a222060 7d17227d 7d>

@top2tech
Copy link
Author

Original string received from the wire:
(lldb) po oriS
swiftR://%7B%22hub%22:%22demohub%22,%22method%22:%22chatMessage%22,%22arguments%22:%7B%22name%22:%22chrome%22,%22message%22:%22%60%7D%17%22%7D%7D

String removed the swiftR prefix
(lldb) po s01
"%7B%22hub%22:%22demohub%22,%22method%22:%22chatMessage%22,%22arguments%22:%7B%22name%22:%22chrome%22,%22message%22:%22%60%7D%17%22%7D%7D"

After webView.stringByEvaluatingJavaScriptFromString, the string is like below
(lldb) po s
"{"hub":"demohub","method":"chatMessage","arguments":{"name":"chrome","message":"`}\u{17}"}}"

adamhartford added a commit that referenced this issue Jul 1, 2015
Support unicode messages from web view.
@adamhartford
Copy link
Owner

Thanks for noticing this. I've changed the way messages are passed from the web view to Swift to support unicode characters. This works with the Chinese characters in your example. Let me know how it works out for you.

@top2tech
Copy link
Author

top2tech commented Jul 1, 2015

great, the issue is fixed. I can send and receive Chinese characters successfully. thanks a lot for the quick turn around !

@top2tech top2tech closed this as completed Jul 1, 2015
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