#include <InjectSocket.h>
Public Member Functions | |
InjectSocket (ISocketHandler &h) | |
~InjectSocket () | |
void | OnConnect () |
void | OnAccept () |
void | SetFilename (const std::string &x) |
void | OnFirst () |
void | OnHeader (const std::string &, const std::string &) |
void | OnHeaderComplete () |
void | OnData (const char *, size_t) |
Private Attributes | |
bool | m_is_server |
std::string | m_filename |
int | m_content_length |
FILE * | m_file |
Definition at line 8 of file InjectSocket.h.
InjectSocket::InjectSocket | ( | ISocketHandler & | h | ) |
Definition at line 14 of file InjectSocket.cpp.
00015 :HTTPSocket(h) 00016 ,m_is_server(false) 00017 ,m_content_length(0) 00018 ,m_file(NULL) 00019 { 00020 }
InjectSocket::~InjectSocket | ( | ) |
void InjectSocket::OnConnect | ( | ) |
Definition at line 30 of file InjectSocket.cpp.
References fil, and m_filename.
00031 { 00032 struct stat st; 00033 if (stat(m_filename.c_str(),&st) == -1) 00034 { 00035 Handler().LogError(this, "stat", errno, strerror(errno), LOG_LEVEL_FATAL); 00036 SetCloseAndDelete(); 00037 return; 00038 } 00039 std::string request; 00040 request += "PUT " + m_filename + " HTTP/1.0\n"; 00041 request += "Content-type: text/xml\n"; 00042 request += "Content-length: " + Utility::l2string(st.st_size) + "\n"; 00043 request += "Connection: close\n"; 00044 request += "\n"; 00045 Send( request ); 00046 FILE *fil; 00047 if ((fil = fopen(m_filename.c_str(),"rb")) != NULL) 00048 { 00049 char buf[1000]; 00050 size_t n; 00051 while ((n = fread(buf,1,1000,fil)) > 0) 00052 { 00053 SendBuf(buf, n); 00054 } 00055 fclose(fil); 00056 } 00057 }
void InjectSocket::OnAccept | ( | ) |
Definition at line 60 of file InjectSocket.cpp.
References m_is_server.
00061 { 00062 m_is_server = true; 00063 }
void InjectSocket::SetFilename | ( | const std::string & | x | ) | [inline] |
void InjectSocket::OnFirst | ( | ) |
void InjectSocket::OnHeader | ( | const std::string & | , | |
const std::string & | ||||
) |
Definition at line 71 of file InjectSocket.cpp.
References m_content_length, and m_filename.
00072 { 00073 // printf("%s: %s\n",key.c_str(),value.c_str()); 00074 if (!strcasecmp(key.c_str(),"content-type")) 00075 { 00076 if (strcasecmp(value.c_str(),"text/xml")) 00077 { 00078 Handler().LogError(this, "OnHeader", 0, "content-type not text/xml", LOG_LEVEL_FATAL); 00079 SetCloseAndDelete(); 00080 } 00081 else 00082 m_filename = GetUrl(); 00083 } 00084 else 00085 if (!strcasecmp(key.c_str(),"content-length")) 00086 { 00087 m_content_length = atoi(value.c_str()); 00088 } 00089 }
void InjectSocket::OnHeaderComplete | ( | ) |
Definition at line 92 of file InjectSocket.cpp.
References m_file, m_filename, and m_is_server.
00093 { 00094 // printf("\n"); 00095 if (m_filename.size() && m_is_server) 00096 { 00097 Uid random; 00098 m_filename = random.GetUid() + ".xml"; 00099 m_file = fopen(m_filename.c_str(),"wb"); 00100 } 00101 }
void InjectSocket::OnData | ( | const char * | , | |
size_t | ||||
) |
Definition at line 104 of file InjectSocket.cpp.
References m_content_length, m_file, and m_filename.
00105 { 00106 // printf("%s",static_cast<std::string>(buf).substr(0,sz).c_str()); 00107 if (m_file) 00108 fwrite(buf,1,sz,m_file); 00109 //printf("content length is still %d bytes, received %d bytes\n",m_content_length,sz); 00110 m_content_length -= sz; 00111 if (m_content_length <= 0) 00112 { 00113 fclose(m_file); 00114 m_file = NULL; 00115 SetCloseAndDelete(); 00116 static_cast<RowoHandler&>(Handler()).InjectRobot(m_filename); 00117 unlink(m_filename.c_str()); 00118 } 00119 }
bool InjectSocket::m_is_server [private] |
std::string InjectSocket::m_filename [private] |
Definition at line 26 of file InjectSocket.h.
Referenced by OnConnect(), OnData(), OnHeader(), OnHeaderComplete(), and SetFilename().
int InjectSocket::m_content_length [private] |
FILE* InjectSocket::m_file [private] |
Definition at line 28 of file InjectSocket.h.
Referenced by OnData(), OnHeaderComplete(), and ~InjectSocket().