|
11 | 11 | #include <netdb.h>
|
12 | 12 | #include "request.h"
|
13 | 13 | #include "fs.h"
|
| 14 | +#include <iomanip> |
14 | 15 | using namespace std;
|
15 | 16 | #define BUFF_SIZE 1024
|
16 | 17 | //#define PORT 9999
|
@@ -138,7 +139,7 @@ std::string createHttpHeader(Method method, const std::string &URI, const std::s
|
138 | 139 |
|
139 | 140 | // Content-Length header
|
140 | 141 | if (contentLen != 0 || method == POST) {
|
141 |
| - request << "Content-Length: " << content.length() << "\r\n"; |
| 142 | + request << "Content-Length: " << contentLen << "\r\n"; |
142 | 143 | }
|
143 | 144 |
|
144 | 145 | // Empty line before the body
|
@@ -210,26 +211,37 @@ int main(int argc, char *argv[]) {
|
210 | 211 | }
|
211 | 212 | string header;
|
212 | 213 | vector<char> file, request;
|
| 214 | + char buf[1024]; |
| 215 | + int n_recv; |
213 | 216 | switch (commandType) {
|
214 | 217 | case Put:
|
215 | 218 | if (!Fs::fileExists(arg)) {
|
216 |
| - cout << "Command failed." << endl; |
| 219 | + cerr << "Command failed." << endl; |
217 | 220 | break;
|
218 | 221 | }
|
219 | 222 | file = Fs::readBinary(arg);
|
220 | 223 | header = createHttpHeader(POST, "/api/file", Fs::getMimeType(arg), file.size());
|
221 | 224 | request = vector<char>(header.begin(), header.end());
|
222 | 225 | for (int i = 0; i < file.size(); i++) request.push_back(file[i]);
|
223 | 226 | send(sockfd, request.data(), request.size(), MSG_NOSIGNAL);
|
224 |
| - int n_recv; |
225 |
| - while ((n_recv = recv(sockfd, buf, sizeof(buf), 0)) > 0) break; |
| 227 | + while ((n_recv = recv(sockfd, buf, sizeof(buf), 0)) > 0) { |
| 228 | + } |
| 229 | + cout << "Command succeeded." << endl; |
| 230 | + break; |
226 | 231 |
|
227 | 232 | case Putv:
|
228 | 233 | if (!Fs::fileExists(arg)) {
|
229 |
| - cout << "Command failed." << endl; |
| 234 | + cerr << "Command failed." << endl; |
230 | 235 | break;
|
231 | 236 | }
|
232 |
| - // /api/video |
| 237 | + file = Fs::readBinary(arg); |
| 238 | + header = createHttpHeader(POST, "/api/video", Fs::getMimeType(arg), file.size()); |
| 239 | + request = vector<char>(header.begin(), header.end()); |
| 240 | + send(sockfd, request.data(), request.size(), MSG_NOSIGNAL); |
| 241 | + int n_recv; |
| 242 | + while ((n_recv = recv(sockfd, buf, sizeof(buf), 0)) > 0) { |
| 243 | + } |
| 244 | + cout << "Command succeeded." << endl; |
233 | 245 | break;
|
234 | 246 |
|
235 | 247 | case Get:
|
|
0 commit comments