c++ - How Do I Set WSABUF.buf For Text And Bin Buffer? -
i'm refactoring code not use std::vector<byte>. how make happen?
somehow, wsasend() prefers have wsabuf.buf pointing std::vector<byte> work image files (.jpg, .png, etc).
during testing, image/* mimetypes return net::err_content_length_mismatch.
byte* httpresponse::getresponse2(ulong *len) { dword dwthreadid = getcurrentthreadid(); char *buffer = (char*)malloc(data_bufsize); memset(buffer, 0, data_bufsize); std::vector<byte> binbuffer = m_sbresponse; std::string ctstr; ctstr.assign(contentype.begin(), contentype.end()); size_t siz = binbuffer.size(); std::string ssiz = std::to_string(siz); strcpy_s(buffer, data_bufsize, resp_ok); strcat_s(buffer, data_bufsize, "\n"); strcat_s(buffer, data_bufsize, "date: "); strcat_s(buffer, data_bufsize, "may 10, 2015"); strcat_s(buffer, data_bufsize, "\n"); strcat_s(buffer, data_bufsize, "content-type: "); strcat_s(buffer, data_bufsize, ctstr.c_str()); strcat_s(buffer, data_bufsize, "\n"); strcat_s(buffer, data_bufsize, "content-length: "); strcat_s(buffer, data_bufsize, ssiz.c_str()); strcat_s(buffer, data_bufsize, "\n"); strcat_s(buffer, data_bufsize, "\n"); int bufsiz = strlen(buffer) + binbuffer.size() + 1; byte* buffer2 = (byte*)malloc(bufsiz); memset(buffer2, 0, bufsiz); strcpy_s((char*)buffer2, strlen(buffer)+1, buffer); int n = strlen((char*)buffer2); std::vector<byte>::iterator it; (it = binbuffer.begin(); != binbuffer.end(); it++) { byte b = *it; buffer2[n++] = b; } *len = strlen((char*)buffer2); return buffer2; }
is there property need set wsa* work i'm trying do?
the file sits in repository.
try more instead:
byte* httpresponse::getresponse2(ulong *len) { *len = 0; std::ostringstream oss; oss << resp_ok << "\n" oss << "date: " << "may 10, 2015" << "\n"; oss << "content-type: " << contentype << "\n"; oss << "content-length: " << m_sbresponse.size() << "\n"; oss << "\n"; std::string s = oss.str(); int bufsiz = s.length() + m_sbresponse.size(); byte* buffer2 = (byte*) malloc(bufsiz); if (buffer2) { std::copy(s.begin(), s.end(), buffer2); std::copy(m_sbresponse.begin(), m_sbresponse.end(), &buffer2[s.length()]); *len = bufsiz; } return buffer2; }
that being said, suggest not returning byte*
. getresponse()
method takes output std::vector<byte>
parameter, should stick model:
void httpresponse::getresponse2(std::vector<byte> *pv) { pv->clear(); std::ostringstream oss; oss << resp_ok << "\n" oss << "date: " << "may 10, 2015" << "\n"; oss << "content-type: " << contentype << "\n"; oss << "content-length: " << m_sbresponse.size() << "\n"; oss << "\n"; std::string s = oss.str(); pv->reserve(s.length() + m_sbresponse.size()); std::copy(s.begin(), s.end(), std::back_inserter(*pv)); std::copy(m_sbresponse.begin(), m_sbresponse.end(), std::back_inserter(*pv)); }