EldoS | Feel safer!

Software components for data protection, secure storage and transfer

Code sample question

Also by EldoS: CallbackDisk
Create virtual disks backed by memory or custom location, expose disk images as disks and more.
#26555
Posted: 09/20/2013 13:41:57
by Katit (Basic support level)
Joined: 09/19/2013
Posts: 14

Not sure why this code needed and so complex looking?
It is found in Receiver project (AS2 sample). When we create receipt - in order to send it back using .NET component we need to extract header and manually apply them to response

Code
// 2. Add each line to the response.Headers
            var stringHeader = SBUtils.__Global.StringOfBytes(binaryHeader);
            var fields = stringHeader.Split(new[] { "\r\n" }, StringSplitOptions.RemoveEmptyEntries);
            var sb = new StringBuilder();
            string temp;
            foreach (string s in fields)
            {
                if (s[0] == (char)9 || s[0] == (char)13)
                {
                    sb.Append(" ");
                    sb.Append(s.TrimStart());
                }
                else
                {
                    if (sb.Length == 0)
                        sb.Append(s);
                    else
                    {
                        temp = sb.ToString();
                        this.WebOperationContext.AddOutgoingHeader(temp);
                        sb = new StringBuilder(s);
                    }
                }
            }

            temp = sb.ToString();
            this.WebOperationContext.AddOutgoingHeader(temp);


For me this works just as well:


Code
// 2. Add each line to the response.Headers
            var stringHeader = SBUtils.__Global.StringOfBytes(binaryHeader);
            var fields = stringHeader.Split(new[] { "\r\n" }, StringSplitOptions.RemoveEmptyEntries);
            foreach (var field in fields)
            {
                this.WebOperationContext.AddOutgoingHeader(field);
            }


Why checking for char(9) and char(13) and why deal with stringbuilder?
#26564
Posted: 09/22/2013 05:59:15
by Alexander Ionov (EldoS Corp.)

Thank you for the question.

The characters #9 and #32 (checking for #13 is a bug - thank you for notice, we'll fix it) are used in the procedure called folding. This procedure is used when it's needed to split a long header field into several shorter lines. In this case at lease one character of #9 (tab) or a #32 (space) are added on the beginning of each line starting from the second line. So a long line
Code
FieldName: very very very long long line

will be folded to
Code
FieldName: very very
  very long long line

The code you're asking about does the unfolding procedure, i.e. it combines several short lines to one long field value.
But if you do not use folding (i.e. TElAS2Message.UseFolding or TElAS2Receipt.UseFolding are set to false) you can simplify this code as you did and this will work fine.


--
Best regards,
Alexander Ionov

Reply

Statistics

Topic viewed 283 times

Number of guests: 1, registered members: 0, in total hidden: 0




|

Back to top

As of July 15, 2016 EldoS Corporation will operate as a division of /n software inc. For more information, please read the announcement.

Got it!