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