public async Task <(HttpStatusCode statusCode, string response)> Save(MultipartItem multipartItem)
        {
            var response = await storage.Save(multipartItem);

            logger.LogInformation($"Response status code: {response.statusCode}");
            if (!IsRetryable(response.statusCode))
            {
                return(response);
            }

            for (var i = 1; i <= options.Retries; i++)
            {
                await Task.Delay(options.WaitBeforeRetryMilliseconds);

                logger.LogInformation($"Forwarding batch items to storage with {i} retry");
                response = await storage.Save(multipartItem);

                logger.LogInformation($"Response status code: {response.statusCode}");
                if (!IsRetryable(response.statusCode))
                {
                    break;
                }
            }

            return(response);
        }
Пример #2
0
        public void GivenAValidContentType_ThenMultipartItemShouldContainTransferSyntaxInHeader(string contentType, string transferSyntax)
        {
            var multipartItem = new MultipartItem(contentType, Stream.Null, transferSyntax);
            var parameters    = (multipartItem.Content.Headers.ContentType.Parameters).ToList();

            Assert.NotNull(parameters.Find(p => p.Value == transferSyntax));
        }
        public static byte[] ToBytes(this MultipartItem item)
        {
            var multipart = new byte[sizeof(short) + item.Data.Length];

            SetMultipartIndex(multipart, (short)item.Index);
            item.Data.CopyTo(multipart, sizeof(short));
            return(multipart);
        }
        private ByteArrayContent ToByteArrayContent(MultipartItem item)
        {
            var content = new ByteArrayContent(item.ToBytes());

            content.Headers.ContentType = new MediaTypeHeaderValue("application/octet-stream");
            content.Headers.ContentEncoding.Add(storageCompressor.ContentEncodingName);
            return(content);
        }
Пример #5
0
        public NetResponse pushMultipartData(String strUrl, MultipartItem oItem, IRhoSession oSession, Hashtable <String, String> pHeaders)
        {
            Vector <MultipartItem> arItems = new Vector <MultipartItem>();

            arItems.addElement(oItem);

            return(pushMultipartData(strUrl, arItems, oSession, pHeaders));
        }
        public async Task <(HttpStatusCode statusCode, string response)> Save(MultipartItem multipartItem)
        {
            var httpClient          = httpClientFactory.CreateClient();
            var uri                 = new Uri(options.Url);
            var httpResponseMessage = await httpClient.PutAsync(uri, ToByteArrayContent(multipartItem));

            return(httpResponseMessage.StatusCode,
                   httpResponseMessage.IsSuccessStatusCode ? await httpResponseMessage.Content.ReadAsStringAsync() : null);
        }
        public void SetValue(string alias, object value, MultipartItem file, string culture = null)
        {
            SetValue(alias, value, culture);

            if (culture == null)
            {
                culture = "$invariant";
            }

            Files[$"{alias}.{culture}"] = file;
        }
 private async Task <(HttpStatusCode statusCode, string response)> TrySave(MultipartItem multipartItem)
 {
     try
     {
         return(await storage.Save(multipartItem));
     }
     catch (Exception exception)
     {
         logger.LogError($"Failed to forward MultipartItem {exception}");
         return(HttpStatusCode.InternalServerError, null);
     }
 }
        public void ToBytes()
        {
            var item = new MultipartItem
            {
                Index = 258,
                Data  = new byte[] { 0, 1, 0 }
            };

            var bytes = item.ToBytes();

            CollectionAssert.AreEqual(new byte[] { 2, 1, 0, 1, 0 }, bytes);
        }
Пример #10
0
        private static void WriteMultipartFormData(Stream stream, string boundary, KeyValuePair <string, object>[] prm)
        {
            var items = prm.ConvertAll(x => MultipartItem.Create(x.Key, x.Value));

            // Start writing
            foreach (var x in items)
            {
                stream.WriteString("--" + boundary + "\r\n");
                x.WriteTo(stream);
                stream.WriteString("\r\n");
            }
            stream.WriteString("--" + boundary + "--");
        }
Пример #11
0
        public async Task <(HttpStatusCode statusCode, string response)> Save(MultipartItem multipartItem)
        {
            data.AddRange(multipartItem.Data);
            if (multipartItem.Index >= 0)
            {
                return(HttpStatusCode.OK, null);
            }

            var items = await Deserialize();

            data.Clear();
            Output(items);

            return(HttpStatusCode.OK, JsonConvert.SerializeObject(new object[0]));
        }
Пример #12
0
        public NetResponse pushMultipartData(String strUrl, MultipartItem oItem, IRhoSession oSession, Hashtable<String,String> pHeaders)
        {
            Vector<MultipartItem> arItems = new Vector<MultipartItem>();
            arItems.addElement(oItem);

            return pushMultipartData(strUrl, arItems, oSession, pHeaders);
        }
Пример #13
0
        //static String szMultipartPostfix =
        //    "\r\n------------A6174410D6AD474183FDE48F5662FCC5--";

        //static String szMultipartContType =
        //    "multipart/form-data; boundary=----------A6174410D6AD474183FDE48F5662FCC5";

        void processMultipartItems(Vector <MultipartItem> arItems)
        {
            for (int i = 0; i < (int)arItems.size(); i++)
            {
                MultipartItem oItem = (MultipartItem)arItems.elementAt(i);

                if (oItem.m_strName.length() == 0)
                {
                    oItem.m_strName = "blob";
                }

                if (oItem.m_strFileName.length() == 0)
                {
                    if (oItem.m_strFilePath.length() > 0)
                    {
                        oItem.m_strFileName = CFilePath.getBaseName(oItem.m_strFilePath);
                    }
                    //else
                    //    oItem.m_strFileName = "doesnotmatter.txt";
                }

                oItem.m_strDataPrefix  = i > 0 ? "\r\n" : "";
                oItem.m_strDataPrefix +=
                    "------------A6174410D6AD474183FDE48F5662FCC5\r\n" +
                    "Content-Disposition: form-data; name=\"";
                oItem.m_strDataPrefix += oItem.m_strName + "\"";
                if (oItem.m_strFileName.length() > 0)
                {
                    oItem.m_strDataPrefix += "; filename=\"" + oItem.m_strFileName + "\"";
                }
                oItem.m_strDataPrefix += "\r\n";
                if (oItem.m_strContentType != null && oItem.m_strContentType.length() > 0)
                {
                    oItem.m_strDataPrefix += "Content-Type: " + oItem.m_strContentType + "\r\n";
                }

                long nContentSize = 0;
                if (oItem.m_strFilePath.length() > 0)
                {
                    //TODO: process file

/*	                SimpleFile file = null;
 *                          try{
 *                                  file = RhoClassFactory.createFile();
 *                                  file.open(oItem.m_strFilePath, true, true);
 *                                  nContentSize = file.length();
 *                                  if ( !file.isOpened() ){
 *                                          LOG.ERROR("File not found: " + oItem.m_strFilePath);
 *                                          throw new RuntimeException("File not found:" + oItem.m_strFilePath);
 *                                  }
 *                          }finally{
 *                                  if ( file != null )
 *                                          try{ file.close(); }catch(IOException e){}
 *                          }*/
                }
                else
                {
                    nContentSize = oItem.m_strBody.length();
                }

                if (oItem.m_strContentType != null && oItem.m_strContentType.length() > 0)
                {
                    oItem.m_strDataPrefix += "Content-Length: " + nContentSize + "\r\n";
                }

                oItem.m_strDataPrefix += "\r\n";
            }
        }
Пример #14
0
        public void GivenAValidContentType_ThenMultipartItemShouldIncludeHeader(string contentType)
        {
            var multipartItem = new MultipartItem(contentType, Stream.Null);

            Assert.Equal(multipartItem.Content.Headers.ContentType.ToString(), contentType);
        }
Пример #15
0
        void processMultipartItems(Vector <MultipartItem> arItems)
        {
            for (int i = 0; i < (int)arItems.size(); i++)
            {
                MultipartItem oItem = (MultipartItem)arItems.elementAt(i);

                if (oItem.m_strName.length() == 0)
                {
                    oItem.m_strName = "blob";
                }

                if (oItem.m_strFileName.length() == 0)
                {
                    if (oItem.m_strFilePath.length() > 0)
                    {
                        oItem.m_strFileName = CFilePath.getBaseName(oItem.m_strFilePath);
                    }
                    //else
                    //    oItem.m_strFileName = "doesnotmatter.txt";
                }

                oItem.m_strDataPrefix  = i > 0 ? "\r\n" : "";
                oItem.m_strDataPrefix +=
                    "------------A6174410D6AD474183FDE48F5662FCC5\r\n" +
                    "Content-Disposition: form-data; name=\"";
                oItem.m_strDataPrefix += oItem.m_strName + "\"";
                if (oItem.m_strFileName.length() > 0)
                {
                    oItem.m_strDataPrefix += "; filename=\"" + oItem.m_strFileName + "\"";
                }
                oItem.m_strDataPrefix += "\r\n";
                if (oItem.m_strContentType != null && oItem.m_strContentType.length() > 0)
                {
                    oItem.m_strDataPrefix += "Content-Type: " + oItem.m_strContentType + "\r\n";
                }

                long nContentSize = 0;
                if (oItem.m_strFilePath.length() > 0)
                {
                    CRhoFile file = null;
                    try{
                        file = RhoClassFactory.createFile();
                        file.open(oItem.m_strFilePath, CRhoFile.EOpenModes.OpenReadOnly);
                        nContentSize = file.size();
                        if (!file.isOpened())
                        {
                            LOG.ERROR("File not found: " + oItem.m_strFilePath);
                            throw new Exception("File not found:" + oItem.m_strFilePath);
                        }
                    }finally{
                        if (file != null)
                        {
                            try{ file.close(); }catch (IOException e)
                            {
                                LOG.ERROR("file closing failed.", e);
                            }
                        }
                    }
                }
                else
                {
                    nContentSize = oItem.m_strBody.length();
                }

                if (oItem.m_strContentType != null && oItem.m_strContentType.length() > 0)
                {
                    oItem.m_strDataPrefix += "Content-Length: " + nContentSize + "\r\n";
                }

                oItem.m_strDataPrefix += "\r\n";
            }
        }
Пример #16
0
        private void GetRequestStreamCallback(IAsyncResult asyncResult)
        {
            Stream stream = null;

            try
            {
                stream = m_webRequest.EndGetRequestStream(asyncResult);
                if (m_strBody != null)
                {
                    stream.Write(new UTF8Encoding().GetBytes(m_strBody), 0, m_strBody.length());//TODO ASCII ???
                }
                else if (m_isMultiPart)
                {
                    for (int i = 0; i < (int)m_arItems.size(); i++)
                    {
                        MultipartItem oItem = (MultipartItem)m_arItems.elementAt(i);
                        stream.Write(new UTF8Encoding().GetBytes(oItem.m_strDataPrefix), 0, oItem.m_strDataPrefix.length());

                        if (oItem.m_strFilePath.length() > 0)
                        {
                            IInputStream fis  = null;
                            CRhoFile     file = RhoClassFactory.createFile();
                            try
                            {
                                file.open(oItem.m_strFilePath, CRhoFile.EOpenModes.OpenReadOnly);
                                if (!file.isOpened())
                                {
                                    LOG.ERROR("File not found: " + oItem.m_strFilePath);
                                    throw new Exception("File not found:" + oItem.m_strFilePath);
                                }

                                fis = file.getInputStream();
                                byte[] byteBuffer = new byte[1024 * 4];
                                int    nRead      = 0;
                                do
                                {
                                    nRead = fis.read(byteBuffer, 0, 1024 * 4);
                                    if (nRead > 0)
                                    {
                                        stream.Write(byteBuffer, 0, nRead);
                                    }
                                } while (nRead > 0);
                            }
                            finally
                            {
                                if (file != null)
                                {
                                    try { file.close(); }
                                    catch (IOException e)
                                    {
                                        LOG.ERROR("GetRequestStreamCallback: file close failed.", e);
                                    }
                                }
                            }
                        }
                        else
                        {
                            stream.Write(new UTF8Encoding().GetBytes(oItem.m_strBody), 0, oItem.m_strBody.length());
                        }
                    }
                    stream.Write(new UTF8Encoding().GetBytes(szMultipartPostfix), 0, szMultipartPostfix.length());
                }
                LOG.INFO("write body done");
            }
            finally
            {
                if (stream != null)
                {
                    stream.Close();
                }
                m_reqWaitEvent.Set();
            }
        }
 public void SetValue(string alias, object value, MultipartItem file)
 {
     SetValue(alias, value);
     Files[alias] = file;
 }