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