Пример #1
0
        public byte[] Decode(HTTPRequestHeaders headers)
        {
            byte[] jsonByteArray = new byte[0];

            if (headers.Exists(Constants.CustomHeader))
            {
                var contextHeader = headers[Constants.CustomHeader];

                jsonByteArray = Convert.FromBase64String(contextHeader);
            }

            return(jsonByteArray);
        }
Пример #2
0
        private bool isJson(HTTPRequestHeaders header)
        {
            if (header == null)
            {
                return(false);
            }

            if (header.Exists("Accept"))
            {
                var type = header["Accept"];

                if (type.ToLower().Contains("json"))
                {
                    return(true);
                }
            }
            return(false);
        }
        // Process POST data

        private void processPOST(Session oS,
                                 ref StringBuilder strbBody,
                                 ref StringBuilder strbHead,
                                 ref int iBinarySequence,
                                 string sFilename)
        {
            HTTPRequestHeaders headers        = oS.RequestHeaders;
            string             strContentType = "";

            // find out if we have a content-type

            if (headers.Exists("Content-Type"))
            {
                strContentType = headers["Content-Type"];
            }
            else
            {
                strContentType = "";
            }

            if (oS.RequestBody.Length > 0)
            {
                // handle different types of data

                // Is it multipart?
                // Or is it binary?
                // /[\x00-\x08\x0E-\x1F\x80-\xFF]

                if (strContentType.Contains("multipart"))
                {
                    strbBody.AppendLine(", \"body\" : `");

                    // Need to write all the multipart stuff to a variable and use it

                    addMultipart(MimeEntity.Load(ContentType.Parse(oS.RequestHeaders["Content-Type"]), new MemoryStream(System.Text.Encoding.UTF8.GetBytes(oS.GetRequestBodyAsString()))),
                                 sFilename,
                                 ref iBinarySequence,
                                 ref strbHead,
                                 ref strbBody);

                    strbBody.AppendLine("`");

                    // Make sure we escape all double quotes " since we use double quotes for strings in the script

                    string strCT = headers["Content-Type"];
                    strCT = strCT.Replace("\"", "\\\"");

                    strbBody.Append(", \"params\" : { headers: { \"Content-Type\" : \"" + strCT + "\"} }");
                }
                else if (Regex.IsMatch(System.Text.Encoding.UTF8.GetString(oS.RequestBody), "[\x00-\x08\x0E-\x1F\x80-\xFF]") || Utilities.IsBinaryMIME(oS.RequestHeaders["Content-Type"]))
                {
                    // Save as binary file and use binary content
                    // same location as Script output since that is allowed to be written
                    // Append open to header before function

                    string strCat = Path.GetDirectoryName(Path.GetFullPath(sFilename));

                    string     strData     = "bin" + iBinarySequence;
                    string     strFilename = strCat + "\\" + strData + ".bin";
                    FileStream f           = new FileStream(strFilename, FileMode.OpenOrCreate, FileAccess.Write);
                    f.Write(oS.RequestBody, 0, oS.RequestBody.Length);
                    f.Close();

                    strbHead.AppendLine(strData + " = open(\"" + strData + ".bin\");");
                    strbHead.AppendLine();
                    strbBody.Append(", \"body\" : " + strData);
                    iBinarySequence++;

                    if (headers.Exists("Content-Type"))
                    {
                        strbBody.Append(", \"params\" : { headers: { \"Content-Type\" : \"" + headers["Content-Type"] + "\"} }");
                    }
                }
                else if (strContentType.Contains("application/x-www-form-urlencoded"))
                {
                    // url-decode and convert string into object for
                    //   - easy JS manipulation
                    //   - readability (have you tried to read URL-encoded?)
                    //   - no need to url-encode manually since k6 methods will do that automagically

                    // Output BODY for POST

                    string strBody = oS.GetRequestBodyAsString();

                    // Split into objects

                    System.Collections.Specialized.NameValueCollection dBody = HttpUtility.ParseQueryString(strBody);

                    // Glue objects back together
                    string strNewBody = "";
                    int    nKeys      = dBody.Keys.Count;
                    int    iKey       = 1;
                    foreach (string key in dBody.Keys)
                    {
                        // Make sure we escape all double quotes " since we use double quotes for strings in the script
                        // If it's the last of the keys, don't add , at the end (we count since order of keys is not guaranteed)
                        if (iKey != nKeys)
                        {
                            strNewBody = strNewBody + "\"" + key + "\":\"" + dBody[key].Replace("\"", "\\\"") + "\",";
                        }
                        else
                        {
                            strNewBody = strNewBody + "\"" + key + "\":\"" + dBody[key].Replace("\"", "\\\"") + "\"";
                        }
                        iKey++;
                    }


                    strbBody.Append(", \"body\" :  {" + strNewBody + "}");
                    if (headers.Exists("Content-Type"))
                    {
                        strbBody.Append(", \"params\" : { headers: { \"Content-Type\" : \"" + headers["Content-Type"] + "\"} }");
                    }
                }
                else
                {
                    // Is text
                    // Output BODY for POST

                    string strBody = oS.GetRequestBodyAsString();

                    // Make sure we escape all double quotes " since we use double quotes for strings in the script

                    strBody = strBody.Replace("\"", "\\\"");

                    strbBody.Append(", \"body\" :  \"" + strBody + "\"");
                    if (headers.Exists("Content-Type"))
                    {
                        strbBody.Append(", \"params\" : { headers: { \"Content-Type\" : \"" + headers["Content-Type"] + "\"} }");
                    }
                }
            }
        }
        // Process Lua POST data

        private void processLuaPOST(Session oS,
                                    ref StringBuilder strbBody,
                                    ref StringBuilder strbHead,
                                    ref int iBinarySequence,
                                    string sFilename)
        {
            HTTPRequestHeaders headers        = oS.RequestHeaders;
            string             strContentType = "";

            // find out if we have a content-type

            if (headers.Exists("Content-Type"))
            {
                strContentType = headers["Content-Type"];
            }
            else
            {
                strContentType = "";
            }

            if (oS.RequestBody.Length > 0)
            {
                // handle different types of data

                // Is it multipart?
                // Or is it binary?
                // /[\x00-\x08\x0E-\x1F\x80-\xFF]

                if (strContentType.Contains("multipart"))
                {
                    strbBody.AppendLine(", data = [[");

                    // Need to write all the multipart stuff to a variable and use it

                    addLuaMultipart(MimeEntity.Load(ContentType.Parse(oS.RequestHeaders["Content-Type"]), new MemoryStream(System.Text.Encoding.UTF8.GetBytes(oS.GetRequestBodyAsString()))),
                                    ref iBinarySequence,
                                    ref strbHead,
                                    ref strbBody);

                    strbBody.AppendLine();
                    strbBody.AppendLine("]]");

                    // Make sure we escape all double quotes " since we use double quotes for strings in the script

                    string strCT = headers["Content-Type"];
                    strCT = strCT.Replace("\"", "\\\"");

                    strbBody.Append(", headers = {[\"Content-Type\"] = \"" + strCT + "\"}");
                }
                else if (Regex.IsMatch(System.Text.Encoding.UTF8.GetString(oS.RequestBody), "[\x00-\x08\x0E-\x1F\x80-\xFF]") || Utilities.IsBinaryMIME(oS.RequestHeaders["Content-Type"]))
                {
                    // Convert to base64-blob
                    // Load in-script
                    // Use base64-blob and convert content-type

                    string strData = "bin" + iBinarySequence;

                    strbHead.AppendLine(strData + " = [[");
                    string binBase64 = Convert.ToBase64String(oS.RequestBody, Base64FormattingOptions.None);
                    strbHead.AppendLine(binBase64);
                    strbHead.AppendLine("]]");
                    strbBody.Append(", data = " + strData);
                    iBinarySequence++;

                    if (headers.Exists("Content-Type"))
                    {
                        strbBody.Append(", headers = {[\"Content-Type\"] = \"" + headers["Content-Type"] + "\", [\"Content-Encoding\"] = \"base64\"}");
                    }
                    else
                    {
                        strbBody.Append(", headers = {[\"Content-Encoding\"] = \"base64\"}");
                    }
                }
                else
                {
                    // Is text
                    // Output BODY for POST

                    string strBody = oS.GetRequestBodyAsString();

                    // Make sure we escape all double quotes " since we use double quotes for strings in the script

                    strBody = strBody.Replace("\"", "\\\"");

                    strbBody.Append(", data =  \"" + strBody + "\"");
                    if (headers.Exists("Content-Type"))
                    {
                        strbBody.Append(", headers = {[\"Content-Type\"] = \"" + headers["Content-Type"] + "\"}");
                    }
                }
            }
        }
Пример #5
0
 public Session SendRequest(HTTPRequestHeaders oHeaders, byte[] arrRequestBodyBytes, StringDictionary oNewFlags, EventHandler<StateChangeEventArgs> onStateChange)
 {
     if (oHeaders.ExistsAndContains("Fiddler-Encoding", "base64"))
     {
         oHeaders.Remove("Fiddler-Encoding");
         if (!Utilities.IsNullOrEmpty(arrRequestBodyBytes))
         {
             arrRequestBodyBytes = Convert.FromBase64String(Encoding.ASCII.GetString(arrRequestBodyBytes));
             if (oNewFlags == null)
             {
                 oNewFlags = new StringDictionary();
             }
             oNewFlags["x-Builder-FixContentLength"] = "CFE-required";
         }
     }
     if (oHeaders.Exists("Fiddler-Host"))
     {
         if (oNewFlags == null)
         {
             oNewFlags = new StringDictionary();
         }
         oNewFlags["x-OverrideHost"] = oHeaders["Fiddler-Host"];
         oNewFlags["X-IgnoreCertCNMismatch"] = "Overrode HOST";
         oHeaders.Remove("Fiddler-Host");
     }
     if ((oNewFlags != null) && oNewFlags.ContainsKey("x-Builder-FixContentLength"))
     {
         if ((arrRequestBodyBytes != null) && !oHeaders.ExistsAndContains("Transfer-Encoding", "chunked"))
         {
             if (!Utilities.HTTPMethodAllowsBody(oHeaders.HTTPMethod) && (arrRequestBodyBytes.Length == 0))
             {
                 oHeaders.Remove("Content-Length");
             }
             else
             {
                 oHeaders["Content-Length"] = arrRequestBodyBytes.LongLength.ToString();
             }
         }
         else
         {
             oHeaders.Remove("Content-Length");
         }
     }
     Session session = new Session((HTTPRequestHeaders) oHeaders.Clone(), arrRequestBodyBytes);
     session.SetBitFlag(SessionFlags.RequestGeneratedByFiddler, true);
     if (onStateChange != null)
     {
         session.OnStateChanged += onStateChange;
     }
     if ((oNewFlags != null) && (oNewFlags.Count > 0))
     {
         foreach (DictionaryEntry entry in oNewFlags)
         {
             session.oFlags[(string) entry.Key] = oNewFlags[(string) entry.Key];
         }
     }
     session.ExecuteUponAsyncRequest();
     return session;
 }