Skip to content

Commit e19f9db

Browse files
committed
add
1 parent 19f4eca commit e19f9db

File tree

2 files changed

+19
-7
lines changed

2 files changed

+19
-7
lines changed

hw2/client.cc

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
#include <netdb.h>
1212
#include "request.h"
1313
#include "fs.h"
14+
#include <iomanip>
1415
using namespace std;
1516
#define BUFF_SIZE 1024
1617
//#define PORT 9999
@@ -138,7 +139,7 @@ std::string createHttpHeader(Method method, const std::string &URI, const std::s
138139

139140
// Content-Length header
140141
if (contentLen != 0 || method == POST) {
141-
request << "Content-Length: " << content.length() << "\r\n";
142+
request << "Content-Length: " << contentLen << "\r\n";
142143
}
143144

144145
// Empty line before the body
@@ -210,26 +211,37 @@ int main(int argc, char *argv[]) {
210211
}
211212
string header;
212213
vector<char> file, request;
214+
char buf[1024];
215+
int n_recv;
213216
switch (commandType) {
214217
case Put:
215218
if (!Fs::fileExists(arg)) {
216-
cout << "Command failed." << endl;
219+
cerr << "Command failed." << endl;
217220
break;
218221
}
219222
file = Fs::readBinary(arg);
220223
header = createHttpHeader(POST, "/api/file", Fs::getMimeType(arg), file.size());
221224
request = vector<char>(header.begin(), header.end());
222225
for (int i = 0; i < file.size(); i++) request.push_back(file[i]);
223226
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;
226231

227232
case Putv:
228233
if (!Fs::fileExists(arg)) {
229-
cout << "Command failed." << endl;
234+
cerr << "Command failed." << endl;
230235
break;
231236
}
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;
233245
break;
234246

235247
case Get:

hw2/makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ CXXFLAGS := -Wall -Wno-unused-variable -Wno-sign-compare -Wno-char-subscripts
55
#-fsanitize=address
66

77
SERVER_SRCS := server.cc request.cc utils.cc utils/base64.c backend.cc html.cc fs.cc response.cc auth.cc
8-
CLIENT_SRCS := client.cc request.cc utils.cc utils/base64.c
8+
CLIENT_SRCS := client.cc request.cc utils.cc utils/base64.c backend.cc html.cc fs.cc response.cc auth.cc
99

1010
all: server client
1111

0 commit comments

Comments
 (0)