Skip to content

Commit deb4b22

Browse files
committed
1 parent 777c183 commit deb4b22

File tree

10 files changed

+156
-15
lines changed

10 files changed

+156
-15
lines changed

u11_testat/FileUploader.cpp

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/*
2+
* File: FileUploader.cpp
3+
* Author: Lukas Elmer
4+
*
5+
* Created on 7. Dezember 2010, 17:17
6+
*/
7+
8+
#include <iterator>
9+
#include <fstream>
10+
#include <stdlib.h>
11+
#include "FileUploader.h"
12+
13+
using namespace std;
14+
15+
FileUploader::FileUploader(string request) {
16+
string file_upload_header("Content-Type: multipart/form-data; boundary=");
17+
int index = request.find(file_upload_header, 0);
18+
cout << index << endl << endl << endl;
19+
if (index > 0) {
20+
//cout << "File uploaded: " << index << endl;
21+
string seperator("\r\n");
22+
int begin_of_boundary_index = index + file_upload_header.size();
23+
string::iterator begin_of_boundary = request.begin() + begin_of_boundary_index;
24+
string boundary(begin_of_boundary, request.find(seperator, begin_of_boundary_index) - begin_of_boundary_index + begin_of_boundary);
25+
int begin_of_boundary_data = request.find(boundary, begin_of_boundary_index + boundary.length()) + boundary.length() + seperator.length();
26+
int end_of_boundary_data = request.find(boundary, begin_of_boundary_data + 1) - seperator.length() - seperator.length();
27+
string raw_file_with_header(request.begin() + begin_of_boundary_data, request.begin() + end_of_boundary_data);
28+
file = UploadFile(raw_file_with_header);
29+
} else {
30+
//cout << "No file uploaded" << index << endl;
31+
}
32+
}
33+
34+
FileUploader::~FileUploader() {
35+
}
36+

u11_testat/FileUploader.h

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
#include <string>
2+
#include <iostream>
3+
4+
#include "UploadFile.h"
5+
6+
#ifndef FILEUPLOADER_H
7+
#defineFILEUPLOADER_H
8+
9+
class FileUploader {
10+
public:
11+
FileUploader(std::string request);
12+
virtual ~FileUploader();
13+
private:
14+
UploadFile file;
15+
};
16+
17+
#endif/* FILEUPLOADER_H */
18+

u11_testat/SocketIO.cpp

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
using namespace std;
44

5-
SocketIO::SocketIO(int fd) : sock(fd), lines(""), /*writePtr(buf), readPtr(buf), endPtr(buf + sizeof (buf)),*/ eof_reached(true) {
5+
SocketIO::SocketIO(int fd) : sock(fd), lines("") {
66
}
77

88
SocketIO::~SocketIO() {
@@ -45,11 +45,8 @@ string SocketIO::getPeerInfo() {
4545
bool SocketIO::fillbuf() { // corresponds to streambuf::underflow()
4646
int i = recv(sock, buf, BUF_SIZE, MSG_DONTWAIT);
4747
if (i <= 0) {
48-
eof_reached = true;
4948
return false;
5049
}
51-
eof_reached = false;
52-
string new_lines(buf, buf + i);
53-
lines += new_lines;
50+
lines += string(buf, buf + i);
5451
return true;
5552
}

u11_testat/SocketIO.h

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,9 @@
1616
class SocketIO {
1717
public:
1818
const static int BUF_SIZE = 1024;
19-
bool eof_reached;
2019

2120
SocketIO(int fd);
2221
virtual ~SocketIO();
23-
//std::string readline();
2422
std::string readlines();
2523
void writeN(const char *buf, int len);
2624
void doClose();
@@ -30,13 +28,7 @@ class SocketIO {
3028
int sock; // socket file descriptor
3129
char buf[BUF_SIZE]; // a bit stupid way to mimick an istream
3230
std::string lines;
33-
// char *writePtr;
34-
// char *readPtr;
35-
// char *endPtr;
36-
//const char *endPtr;
37-
3831
bool fillbuf();
39-
// int getc();
4032
};
4133

4234
#endif/* SOCKETIO_H */

u11_testat/UploadFile.cpp

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
/*
2+
* File: UploadFile.cpp
3+
* Author: Lukas Elmer
4+
*
5+
* Created on 7. Dezember 2010, 18:49
6+
*/
7+
8+
#include <iosfwd>
9+
#include <string>
10+
#include <fstream>
11+
#include <iostream>
12+
#include <iterator>
13+
#include <stdlib.h>
14+
15+
#include "UploadFile.h"
16+
17+
using namespace std;
18+
19+
UploadFile::UploadFile() : file_name(""), file_raw(""), file_with_header("") {
20+
}
21+
22+
UploadFile::UploadFile(string _file_with_header) : file_name(""), file_raw(""), file_with_header("") {
23+
file_with_header = _file_with_header;
24+
processFileWithHeader();
25+
saveFile();
26+
}
27+
28+
UploadFile::~UploadFile() {
29+
}
30+
31+
void UploadFile::processFileWithHeader() {
32+
//file_data_with_header;
33+
string double_seperator("\r\n\r\n");
34+
file_raw = string(file_with_header.begin() + file_with_header.find(double_seperator) + double_seperator.length(), file_with_header.end());
35+
//cout << "ZZZZZZZZZZZZZZ" << endl << file_with_header.find("\n") << endl << endl << file_raw << endl << endl << endl;
36+
file_name = "xxxx.png";
37+
}
38+
39+
void UploadFile::saveFile() {
40+
ofstream f;
41+
f.open(file_name.c_str());
42+
f << file_raw;
43+
f.close();
44+
}

u11_testat/UploadFile.h

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
#include <string>
2+
#include <iostream>
3+
4+
#ifndef UPLOADFILE_H
5+
#defineUPLOADFILE_H
6+
7+
class UploadFile {
8+
public:
9+
UploadFile();
10+
UploadFile(std::string file_with_header);
11+
virtual ~UploadFile();
12+
private:
13+
std::string file_with_header;
14+
std::string file_name;
15+
std::string file_raw;
16+
void processFileWithHeader();
17+
void saveFile();
18+
};
19+
20+
#endif/* UPLOADFILE_H */
21+

u11_testat/htechoserv.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

1414
#include "ServerSocket.h"
1515
#include "SocketIO.h"
16+
#include "FileUploader.h"
1617

1718
using namespace std;
1819

@@ -77,6 +78,10 @@ int main(int argc, char**argv) {
7778
cout << "Connected to " << sio.getPeerInfo() << endl;
7879
string lines_received = sio.readlines();
7980
sio.closeReadSocket();
81+
82+
// Just for fun: Upload file
83+
FileUploader f(lines_received);
84+
8085
if (quitRequested(lines_received)) {
8186
cout << endl << endl;
8287
quit = true;

u11_testat/nbproject/Makefile-Debug.mk

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,9 @@ OBJECTDIR=build/${CND_CONF}/${CND_PLATFORM}
3333

3434
# Object Files
3535
OBJECTFILES= \
36+
${OBJECTDIR}/UploadFile.o \
3637
${OBJECTDIR}/SocketIO.o \
38+
${OBJECTDIR}/FileUploader.o \
3739
${OBJECTDIR}/ServerSocket.o \
3840
${OBJECTDIR}/htechoserv.o
3941

@@ -62,11 +64,21 @@ dist/Debug/Cygwin_4.x-Windows/u11_testat.exe: ${OBJECTFILES}
6264
${MKDIR} -p dist/Debug/Cygwin_4.x-Windows
6365
${LINK.cc} -o ${CND_DISTDIR}/${CND_CONF}/${CND_PLATFORM}/u11_testat ${OBJECTFILES} ${LDLIBSOPTIONS}
6466

67+
${OBJECTDIR}/UploadFile.o: nbproject/Makefile-${CND_CONF}.mk UploadFile.cpp
68+
${MKDIR} -p ${OBJECTDIR}
69+
${RM} $@.d
70+
$(COMPILE.cc) -g -MMD -MP -MF $@.d -o ${OBJECTDIR}/UploadFile.o UploadFile.cpp
71+
6572
${OBJECTDIR}/SocketIO.o: nbproject/Makefile-${CND_CONF}.mk SocketIO.cpp
6673
${MKDIR} -p ${OBJECTDIR}
6774
${RM} $@.d
6875
$(COMPILE.cc) -g -MMD -MP -MF $@.d -o ${OBJECTDIR}/SocketIO.o SocketIO.cpp
6976

77+
${OBJECTDIR}/FileUploader.o: nbproject/Makefile-${CND_CONF}.mk FileUploader.cpp
78+
${MKDIR} -p ${OBJECTDIR}
79+
${RM} $@.d
80+
$(COMPILE.cc) -g -MMD -MP -MF $@.d -o ${OBJECTDIR}/FileUploader.o FileUploader.cpp
81+
7082
${OBJECTDIR}/ServerSocket.o: nbproject/Makefile-${CND_CONF}.mk ServerSocket.cpp
7183
${MKDIR} -p ${OBJECTDIR}
7284
${RM} $@.d

u11_testat/nbproject/Makefile-Release.mk

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,9 @@ OBJECTDIR=build/${CND_CONF}/${CND_PLATFORM}
3333

3434
# Object Files
3535
OBJECTFILES= \
36+
${OBJECTDIR}/UploadFile.o \
3637
${OBJECTDIR}/SocketIO.o \
38+
${OBJECTDIR}/FileUploader.o \
3739
${OBJECTDIR}/ServerSocket.o \
3840
${OBJECTDIR}/htechoserv.o
3941

@@ -62,11 +64,21 @@ dist/Release/Cygwin_4.x-Windows/u11_testat.exe: ${OBJECTFILES}
6264
${MKDIR} -p dist/Release/Cygwin_4.x-Windows
6365
${LINK.cc} -o ${CND_DISTDIR}/${CND_CONF}/${CND_PLATFORM}/u11_testat ${OBJECTFILES} ${LDLIBSOPTIONS}
6466

67+
${OBJECTDIR}/UploadFile.o: nbproject/Makefile-${CND_CONF}.mk UploadFile.cpp
68+
${MKDIR} -p ${OBJECTDIR}
69+
${RM} $@.d
70+
$(COMPILE.cc) -O2 -MMD -MP -MF $@.d -o ${OBJECTDIR}/UploadFile.o UploadFile.cpp
71+
6572
${OBJECTDIR}/SocketIO.o: nbproject/Makefile-${CND_CONF}.mk SocketIO.cpp
6673
${MKDIR} -p ${OBJECTDIR}
6774
${RM} $@.d
6875
$(COMPILE.cc) -O2 -MMD -MP -MF $@.d -o ${OBJECTDIR}/SocketIO.o SocketIO.cpp
6976

77+
${OBJECTDIR}/FileUploader.o: nbproject/Makefile-${CND_CONF}.mk FileUploader.cpp
78+
${MKDIR} -p ${OBJECTDIR}
79+
${RM} $@.d
80+
$(COMPILE.cc) -O2 -MMD -MP -MF $@.d -o ${OBJECTDIR}/FileUploader.o FileUploader.cpp
81+
7082
${OBJECTDIR}/ServerSocket.o: nbproject/Makefile-${CND_CONF}.mk ServerSocket.cpp
7183
${MKDIR} -p ${OBJECTDIR}
7284
${RM} $@.d

u11_testat/nbproject/configurations.xml

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@
55
displayName="Header Files"
66
projectFiles="true"
77
kind="SOURCE_LOGICAL_FOLDER">
8+
<itemPath>FileUploader.h</itemPath>
9+
<itemPath>ServerSocket.h</itemPath>
10+
<itemPath>SocketIO.h</itemPath>
811
</logicalFolder>
912
<logicalFolder name="ResourceFiles"
1013
displayName="Resource Files"
@@ -15,10 +18,11 @@
1518
displayName="Source Files"
1619
projectFiles="true"
1720
kind="SOURCE_LOGICAL_FOLDER">
21+
<itemPath>FileUploader.cpp</itemPath>
1822
<itemPath>ServerSocket.cpp</itemPath>
19-
<itemPath>ServerSocket.h</itemPath>
2023
<itemPath>SocketIO.cpp</itemPath>
21-
<itemPath>SocketIO.h</itemPath>
24+
<itemPath>UploadFile.cpp</itemPath>
25+
<itemPath>UploadFile.h</itemPath>
2226
<itemPath>htechoserv.cpp</itemPath>
2327
</logicalFolder>
2428
<logicalFolder name="TestFiles"

0 commit comments

Comments
 (0)