Ejemplo n.º 1
0
        public static void http_get(Stream stream, string filename, Uri uri, thinCLIProtocol tCLIprotocol)
        {
            try
            {
                if (File.Exists(filename))
                {
                    throw new Exception(string.Format("The file '{0}' already exists", filename));
                }

                using (FileStream fs = new FileStream(filename, FileMode.Create, FileAccess.Write))
                {
                    using (Stream http = HTTP.doRPC("GET", uri, tCLIprotocol))
                    {
                        if (http == null)
                        {
                            tCLIprotocol.dGlobalError("Server rejected request");
                            marshal_response(stream, tag.Failed);
                            return;
                        }
                        byte[] block = new byte[tCLIprotocol.conf.block_size];
                        while (true)
                        {
                            int n = http.Read(block, 0, block.Length);
                            if (n == 0)
                            {
                                break;
                            }
                            fs.Write(block, 0, n);
                        }
                        marshal_response(stream, tag.OK);
                    }
                }
            }
            catch (Exception e)
            {
                tCLIprotocol.dGlobalError("Received exception: " + e.Message);
                tCLIprotocol.dGlobalError("Unable to write output file: " + filename);
                marshal_response(stream, tag.Failed);
            }
        }
Ejemplo n.º 2
0
 public static void http_put(Stream stream, string filename, Uri uri, thinCLIProtocol tCLIprotocol)
 {
     try
     {
         using (FileStream fs = new FileStream(filename, FileMode.Open, FileAccess.Read))
         {
             using (Stream http = HTTP.doRPC("PUT", uri, tCLIprotocol,
                                             string.Format("Content-Length: {0}", fs.Length)))
             {
                 if (http == null)
                 {
                     marshal_response(stream, tag.Failed);
                     return;
                 }
                 byte[] block = new byte[tCLIprotocol.conf.block_size];
                 while (true)
                 {
                     int n = fs.Read(block, 0, block.Length);
                     if (n == 0)
                     {
                         break;
                     }
                     http.Write(block, 0, n);
                 }
                 marshal_response(stream, tag.OK);
             }
         }
     }
     catch (FileNotFoundException)
     {
         tCLIprotocol.dGlobalError("File not found");
         marshal_response(stream, tag.Failed);
     }
     catch (Exception e)
     {
         tCLIprotocol.dGlobalError(string.Format("Received exception: {0}", e.Message));
         marshal_response(stream, tag.Failed);
     }
 }