Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 13 additions & 2 deletions apps/webget.cc
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,19 @@ using namespace std;

void get_URL( const string& host, const string& path )
{
cerr << "Function called: get_URL(" << host << ", " << path << ")\n";
cerr << "Warning: get_URL() has not been implemented yet.\n";
// cerr << "Function called: get_URL(" << host << ", " << path << ")\n";
// cerr << "Warning: get_URL() has not been implemented yet.\n";
TCPSocket tcpsock;
tcpsock.connect(Address(host ,"http"));
string request_header = "GET " + path + " HTTP/1.1\r\nHost: " + host + "\r\nConnection: close\r\n\r\n";
tcpsock.write(request_header);
string buf;
while(!tcpsock.eof()){
buf.clear();
tcpsock.read(buf);
cout << buf;
}
tcpsock.shutdown(SHUT_RDWR);
}

int main( int argc, char* argv[] )
Expand Down
29 changes: 19 additions & 10 deletions src/byte_stream.cc
Original file line number Diff line number Diff line change
Expand Up @@ -2,64 +2,73 @@

using namespace std;

ByteStream::ByteStream( uint64_t capacity ) : capacity_( capacity ) {}
ByteStream::ByteStream( uint64_t capacity ) : capacity_( capacity ) ,closed_(false),
total_pushed_(0), total_poped_(0), buffer(){}

bool Writer::is_closed() const
{
// Your code here.
return {};
return closed_;
}

void Writer::push( string data )
{
// Your code here.
(void)data;
if(is_closed() || has_error() || data.empty()){
return;
}
LEN_T l = min(data.size(), available_capacity());
buffer.append(data.substr(0, l));
total_pushed_ += l;
return;
}

void Writer::close()
{
// Your code here.
closed_ = true;
}

uint64_t Writer::available_capacity() const
{
// Your code here.
return {};
return capacity_ - buffer.size();
}

uint64_t Writer::bytes_pushed() const
{
// Your code here.
return {};
return total_pushed_;
}

bool Reader::is_finished() const
{
// Your code here.
return {};
return closed_ && bytes_buffered() == 0;
}

uint64_t Reader::bytes_popped() const
{
// Your code here.
return {};
return total_poped_;
}

string_view Reader::peek() const
{
// Your code here.
return {};
return {buffer};
}

void Reader::pop( uint64_t len )
{
// Your code here.
(void)len;
LEN_T l = min(len, buffer.size());
buffer.erase(0, l);
total_poped_ += l;
}

uint64_t Reader::bytes_buffered() const
{
// Your code here.
return {};
return buffer.size();
}
8 changes: 8 additions & 0 deletions src/byte_stream.hh
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,14 @@
#include <string>
#include <string_view>

using namespace std;
class Reader;
class Writer;

class ByteStream
{
public:
using LEN_T = uint64_t;
explicit ByteStream( uint64_t capacity );

// Helper functions (provided) to access the ByteStream's Reader and Writer interfaces
Expand All @@ -25,6 +27,12 @@ protected:
// Please add any additional state to the ByteStream here, and not to the Writer and Reader interfaces.
uint64_t capacity_;
bool error_ {};

bool closed_;
LEN_T total_pushed_;
LEN_T total_poped_;
string buffer;

};

class Writer : public ByteStream
Expand Down