Flex + character getting replaced by blank when passed as application param - apache-flex

I am passing a string param into flex application as FlashVars
The param sometime contains special characters, everything shows fine except + sign which gets replaced by blank when it reaches flex.
Here is the scenario for eg.
I have a local variable in JS that gets some values and for this example it can be taken as following
<script language="JavaScript" type="text/javascript">
var testVar = "some_test_string_that_contains_+_character";
</script>
Then to my Flex object AC_FL_RunContent I pass "FlashVars","test="+testVar
In the flex code on Init of application I have written
var testValue:String = application.parameters["testVar"].toString();
Alert.show(testValue);
Now this shows fine except the + character gets replaced by blank. It doesnt happens with other characters like /,#$-_ but the + character.
Any help regarding this will be greatly appreciated.

From Adobe's docs:
About flashVars properties encoding
The values of the flashVars properties
must be URL encoded. The format of the
string is a set of name-value pairs
separated by an ampersand (&). You can
escape special and nonprintable
characters with a percent symbol (%)
followed by a two-digit hexadecimal
value. You can represent a single
blank space using the plus sign (+).
Looking further down at one of the comments from matthew horn, it looks like you can use %2B to pass the + character.

Related

Restrict user input to characters in IBM System i 00280 code page

We need to restrict user input in a classic ASP web site to the characters allowed by the 00280 code page of IBM System i.
Is there a way to do it in a sane way besides having a (JavaScript|VBScript) function checking every character of an input string against a string of allowed characters?
A basic classic ASP function I thought of:
Function CheckInput(text, replacement)
Dim output : output = ""
Dim haystack : haystack = "abcd.. " ' Insert here the allowed characters.
Dim i : i = 0
For i = 1 To Len(text)
Dim needle : needle = Mid(text, i, 1)
If InStr(haystack, needle) = 0 Then
needle = replacement
End If
output = output & needle
Next
CheckInput = output
End Function
Would - in my function - a RegExp be an overkill?
The short answer to your first question is: No. To your second question: RegEx might not help you here because not all RegEx implementation in browsers will support the characters you need to test and neither does VBScript version of RegEx.
Even using the code approach you are proposing would need some very careful thought. In order to be able to place the set of characters you want to support in as string literal the codepage that you save the ASP file would need to be one that covers all the characters needed or alternatively you would need to use AscW to help you build a string containing those characters.
One slightly simpler approach would be to use Javascript and have the page charset and codepage set to UTF-8. This would allow you to create a string literal containing anyset of characters.
Since it is generally not considered secure to rely on browser validation, you should consider changing your IBM i (formerly OS/400) application interface to accept UCS-2 data, and perform any necessary validation and conversion at the server side.

VB.NET : FromBase64String Error

I am getting following error when I am trying to use Convert.FromBase64String
"The input is not a valid Base-64 string as it contains a non-base 64 character, more than two padding characters, or a non-white space character among the padding characters."
Dim payloadBytes = Convert.FromBase64String(payloadBase64)
Basically when my facebook registration form (http://developers.facebook.com/docs/plugins/registration/) phone field has a dash in it and encoded string is posted to other page and I am trying to decode it there which creates this error. Basically I am trying to extract data from Facebook Signed Request.
The issue is that the dash is not a valid character in the Base64String. Here is a quote from MSDN:
The base-64 digits in ascending order from zero are the uppercase characters "A" to "Z", the lowercase characters "a" to "z", the numerals "0" to "9", and the symbols "+" and "/". The valueless character, "=", is used for trailing padding.
You can either take the dash out (which might not be what you want) or you need to figure out what format the data is truly coming in as since it doesn't seem like it is Base-64 string data.
http://msdn.microsoft.com/en-us/library/system.convert.frombase64string.aspx
The issue is that the Facebook Signed Request is using a modified Base-64 request for URL that changes a few things. Here is a quote on what it does:
For this reason, a modified Base64 for URL variant exists, where no padding '=' will be used, and the '+' and '/' characters of standard Base64 are respectively replaced by '-' and '_', so that using URL encoders/decoders are no longer necessary and have no impact on the length of the encoded value, leaving the same encoded form intact for use in relational databases, web forms, and object identifiers in general.
I believe you could solve your problem by simply replace the dash with a plus and replace the underscore with a backslash and you should be able to then decode it from Base-64.
Here is the link to the Facebook developers page that indicates that the value you are trying to decode is base64url encoded:
http://developers.facebook.com/docs/authentication/signed_request/
Are you trying to encode it to Base64 OR decode something encoded to Base64? From the looks of it, you should use Convert.ToBase64String.
- is definitely not a character that will appear in Base64 encoded string.
Are you sure that you are getting a valid Base64 encoded string from Facebook Signed Request ?
I also got same problem on same task
I was using ifram, everything was working fine, then after that on the same page I replaced ifrm code with xfbml registration code, when I checked it again it was giving following error
"The input is not a valid Base-64 string as it contains a non-base 64 character, more than two padding characters, or a non-white space character among the padding characters."
I spent lot of time to fix this issue but problem was still there, at the end I think about temporary internet files and I delete clear those files after that when I tested it was working fine.
You can also try this solution.
signedfor vb asp.net you can do this to extract and decode the payload from facebook send request, I hope this helps because I'm not familiar with vb and asp.net and spent a lot of time figuring out why I was getting the same error you were.
<%# Page Language="vb" %>
<%
Dim strSignedRequest As String
strSignedRequest = Request("signed_request")
If String.IsNullOrEmpty(strSignedRequest) = False Then
Dim arrayRequest As Array
arrayRequest = Split(strSignedRequest, ".")
Dim strPayload As String
strPayload = arrayRequest(1)
strPayload = Replace(strPayload, "-", "+")
strPayload = Replace(strPayload, "_", "/")
' padding, FromBase64String() will barf if the string is the wrong length so we need to pad it with =
strPayload = strPayload.PadRight(strPayload.Length + (4 - strPayload.Length Mod 4) Mod 4, "="C)
Dim bytSignedRequest As Byte()
bytSignedRequest = Convert.FromBase64String(strPayload)
Dim strJson As String
strJson = Encoding.UTF8.GetString(bytSignedRequest)
'Response.Write("encoded: " & strPayload)
Response.Write("decoded: " & strJson)
End If
%>

Convert characters to html equivalent using .net

I have a text document that is a roster of licensees. I am looping through this document to create a html table of this data. I've come across names with non standard characters.
This is one of them
Aimeé
I tried running all the inputs through the following function, but when it comes across the above character it doesn't replace it.
Function ReplaceBadCharacters(ByVal input As String) As String
Return input.Replace(Chr(233), "é")
End Function
How can I replace each character with the html equivalent?
EDIT
When I debug the above function it shows the input as Aime[] and not Aimeé.
In Chrome it looks like this Aime�
You don't need to do that.
As long as your page is encoded as UTF8, the characters will work fine.
However, you do need to call Server.HtmlEncode to escape HTML special characters.
(Unless you're printing the strings in a <%: %> block or a Razor # block, which escapes them for you)
é is in the current ASCII char set. If you put that into the HTML, it will render correctly (just like how it shows up correctly in the browser when you look at this page)
but if you want to replace all instances of it, use this instead é
input.Replace("é", "é")

HttpServerUtility.UrlPathEncode vs HttpServerUtility.UrlEncode

What's the difference between HttpServerUtility.UrlPathEncode and HttpServerUtility.UrlEncode? And when should I choose one over the other?
UrlEncode is useful for query string values (so to the left or especially, right, of each =).
In this url, foo, fooval, bar, and barval should EACH be UrlEncode'd separately:
http://www.example.com/whatever?foo=fooval&bar=barval
UrlEncode encodes everything, such as ?, &, =, and /, accented or other non-ASCII characters, etc, into %-style encoding, except space which it encodes as a +. This is form-style encoding, and is best for something you intend to put in the querystring (or maybe between two slashes in a url) as a parameter without it getting all jiggy with the url's control characters (like &). Otherwise an unfortunately placed & or = in a user's form input or db value value could break things.
EDIT: Uri.EscapeDataString is a very close match to UrlEncode, and may be preferable, though I don't know the exact differences.
UrlPathEncode is useful for the rest of the query string, it affects everything to the left of the ?.
In this url, the entire url (from http to barval) should be run through UrlPathEncode.
http://www.example.com/whatever?foo=fooval&bar=barval
UrlPathEncode does NOT encode ?, &, =, or /. It DOES, however, like UrlEncode, encode accented/non-ASCII characters with % notation, and space also becomes %20. This is useful to make sure the url is valid, since spaces and accented characters are not. It won't touch your querystring (everything to the right of ?), so you have to encode that with UrlEncode, above.
Update: as of 4.5, per MSDN reference, Microsoft recommends to only use UrlEncode. Also, the information previously listed in MSDN does not fully describe behavior of the two methods - see comments.
The difference is all in the space escaping - UrlEncode escapes them into + sign, UrlPathEncode escapes into %20. + and %20 are only equivalent if they are part of QueryString portion per W3C. So you can't escape whole URL using + sign, only querystring portion. Bottom line is that UrlPathEncode is always better imho
You can encode a URL using with the UrlEncode() method or the UrlPathEncode() method. However, the methods return different results. The UrlEncode() method converts each space character to a plus character (+). The UrlPathEncode() method converts each space character into the string "%20", which represents a space in hexadecimal notation. Use the UrlPathEncode() method when you encode the path portion of a URL in order to guarantee a consistent decoded URL, regardless of which platform or browser performs the decoding.
http://msdn.microsoft.com/en-us/library/4fkewx0t.aspx
To explain it as simply as possible:
HttpUtility.UrlPathEncode("http://www.foo.com/a b/?eggs=ham&bacon=1")
becomes
http://www.foo.com/a%20b/?eggs=ham&bacon=1
and
HttpUtility.UrlEncode("http://www.foo.com/a b/?eggs=ham&bacon=1")
becomes
http%3a%2f%2fwww.foo.com%2fa+b%2f%3feggs%3dham%26bacon%3d1

Character Support Issue - How to Translate Higher ASCII Characters to Lower ASCII Characters

So I have an ASP.Net (vb.net) application. It has a textbox and the user is pasting text from Microsoft Word into it. So things like the long dash (charcode 150) are coming through as input. Other examples would be the smart quotes or accented characters. In my app I'm encoding them in xml and passing that to the database as an xml parameter to a sql stored procedure. It gets inserted in the database just as the user entered it.
The problem is the app that reads this data doesn't like these characters. So I need to translate them into the lower ascii (7bit I think) character set. How do I do that? How do I determine what encoding they are in so I can do something like the following. And would just requesting the ASCII equivalent translate them intelligently or do I have to write some code for that?
Also maybe it might be easier to solve this problem in the web page to begin with. When you copy the selection of characters from Word it puts several formats in the clipboard. The straight text one is the one I want. Is there a way to have the html textbox get that text when the user pastes into it? Do I have to set the encoding of the web page somehow?
System.Text.Encoding.ASCII.GetString(System.Text.Encoding.GetEncoding(1251).GetBytes(text))
Code from the app that encodes the input into xml:
Protected Function RequestStringItem( _
ByVal strName As System.String) As System.String
Dim strValue As System.String
strValue = Me.Request.Item(strName)
If Not (strValue Is Nothing) Then
RequestStringItem = strValue.Trim()
Else
RequestStringItem = ""
End If
End Function
' I get the input from the textboxes into an array like this
m_arrInsertDesc(intIndex) = RequestStringItem("txtInsertDesc" & strValue)
m_arrInsertFolder(intIndex) = RequestInt32Item("cboInsertFolder" & strValue)
' create xml file for inserts
strmInsertList = New System.IO.MemoryStream()
wrtInsertList = New System.Xml.XmlTextWriter(strmInsertList, System.Text.Encoding.Unicode)
' start document and add root element
wrtInsertList.WriteStartDocument()
wrtInsertList.WriteStartElement("Root")
' cycle through inserts
For intIndex = 0 To m_intInsertCount - 1
' if there is an insert description
If m_arrInsertDesc(intIndex).Length > 0 Then
' if the insert description is of the appropriate length
If m_arrInsertDesc(intIndex).Length <= 96 Then
' add element to xml
wrtInsertList.WriteStartElement("Insert")
wrtInsertList.WriteAttributeString("insertdesc", m_arrInsertDesc(intIndex))
wrtInsertList.WriteAttributeString("insertfolder", m_arrInsertFolder(intIndex).ToString())
wrtInsertList.WriteEndElement()
' if insert description is too long
Else
m_strError = "ERROR: INSERT DESCRIPTION TOO LONG"
Exit Function
End If
End If
Next
' close root element and document
wrtInsertList.WriteEndElement()
wrtInsertList.WriteEndDocument()
wrtInsertList.Close()
' when I add the xml as a parameter to the stored procedure I do this
cmdAddRequest.Parameters.Add("#insert_list", OdbcType.NText).Value = System.Text.Encoding.Unicode.GetString(strmInsertList.ToArray())
How big is the range of these input characters? 256? (each char fits into a single byte). If that's true, it wouldn't be hard to implement a 256 value lookup table. I haven't toyed with BASIC in years, but basically you'd DIM an array of 256 bytes and fill in the array with translated values, i.e. the 'a'th byte would get 'a' (since it's OK as is) but the 150'th byte would get a hyphen.
This seems to work for long dash to short dash and smart quotes to regular quotes. As my html pages has the following as the content type. But it converts all the accented characters to questions marks. Which is not what the Text version of the clipboard has. So I'm closer, I just think I have the target encoding wrong.
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
System.Text.Encoding.ASCII.GetString(System.Text.Encoding.GetEncoding("iso-8859-1").GetBytes(m_arrFolderDesc(intIndex)))
Edit: Found the correct target encoding for my purposes which is 1252.
System.Text.Encoding.GetEncoding(1252).GetString(System.Text.Encoding.GetEncoding("iso-8859-1").GetBytes(m_arrFolderDesc(intIndex)))
If you convert to a non-unicode character set, you will lose some characters in the process. If the legacy app reading the data doesn't need to do any string transformations, you might want to consider using UTF-7, and converting it back once it gets back into the unicode world - this will preserve all special characters.

Resources