00001
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032 #ifdef _WIN32
00033 #ifdef _MSC_VER
00034 #pragma warning(disable:4786)
00035 #endif
00036 #else
00037 #include <errno.h>
00038 #endif
00039 #include "ISocketHandler.h"
00040 #include <sys/types.h>
00041 #include <sys/stat.h>
00042 #include "File.h"
00043
00044 #include "HttpPutSocket.h"
00045
00046
00047 #ifdef SOCKETS_NAMESPACE
00048 namespace SOCKETS_NAMESPACE {
00049 #endif
00050
00051
00052 HttpPutSocket::HttpPutSocket(ISocketHandler& h) : HttpClientSocket(h)
00053 {
00054 }
00055
00056
00057 HttpPutSocket::HttpPutSocket(ISocketHandler& h,const std::string& url_in) : HttpClientSocket(h, url_in)
00058 {
00059 }
00060
00061
00062 HttpPutSocket::~HttpPutSocket()
00063 {
00064 }
00065
00066
00067 void HttpPutSocket::SetFile(const std::string& file)
00068 {
00069 struct stat st;
00070 if (!stat(file.c_str(), &st))
00071 {
00072 m_filename = file;
00073 m_content_length = st.st_size;
00074 }
00075 else
00076 {
00077 Handler().LogError(this, "SetFile", Errno, StrError(Errno), LOG_LEVEL_FATAL);
00078 SetCloseAndDelete();
00079 }
00080 }
00081
00082
00083 void HttpPutSocket::SetContentType(const std::string& type)
00084 {
00085 m_content_type = type;
00086 }
00087
00088
00089
00090 void HttpPutSocket::Open()
00091 {
00092
00093 TcpSocket::Open(GetUrlHost(), GetUrlPort());
00094 }
00095
00096
00097 void HttpPutSocket::OnConnect()
00098 {
00099 SetMethod( "PUT" );
00100 SetHttpVersion( "HTTP/1.1" );
00101 AddResponseHeader( "Host", GetUrlHost() );
00102 AddResponseHeader( "Content-type", m_content_type );
00103 AddResponseHeader( "Content-length", Utility::l2string(m_content_length) );
00104 AddResponseHeader( "User-agent", MyUseragent() );
00105 SendRequest();
00106
00107 std::auto_ptr<IFile> fil = std::auto_ptr<IFile>(new File);
00108 if (fil -> fopen(m_filename, "rb"))
00109 {
00110 size_t n;
00111 char buf[32768];
00112 while ((n = fil -> fread(buf, 1, 32768)) > 0)
00113 {
00114 SendBuf(buf, n);
00115 }
00116 fil -> fclose();
00117 }
00118 }
00119
00120
00121 #ifdef SOCKETS_NAMESPACE
00122 }
00123 #endif
00124
00125