Skip to content

Commit 053c091

Browse files
committed
Fixed various kqueue issues. Sending large files on both Linux and BSD systems now works properly.
1 parent 73c7b5d commit 053c091

23 files changed

+246
-166
lines changed

LICENSE.TXT

100644100755
File mode changed.

Makefile

100644100755
Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# Makefile for httpserver
22
# (C) Ramsey Kant 2011-2012
33

4-
CC := clang++
4+
CC := g++
55
SRCDIR := src
66
BINDIR := bin
77
BUILDDIR := build
@@ -10,11 +10,18 @@ TARGET := httpserver
1010
# Debug Flags
1111
DEBUGFLAGS := -g3 -O0 -Wall
1212
RTCHECKS := -fmudflap -fstack-check -gnato
13+
1314
# Production Flags
1415
PRODFLAGS := -Wall -O2
15-
# Active Flags
16-
CFLAGS := -std=c++11 -stdlib=libc++ -Iinclude/ $(DEBUGFLAGS)
17-
LINK := -stdlib=libc++ $(DEBUGFLAGS)
16+
17+
# OSX Flags
18+
#CFLAGS := -std=c++11 -stdlib=libc++ -Iinclude/ $(DEBUGFLAGS)
19+
#LINK := -stdlib=libc++ $(DEBUGFLAGS)
20+
21+
# Linux Flags
22+
CFLAGS := -std=c++11 -Iinclude/ $(DEBUGFLAGS)
23+
LINK := -lpthread $(DEBUGFLAGS) -lkqueue
24+
1825

1926
SRCEXT := cpp
2027
SOURCES := $(shell find $(SRCDIR) -type f -name *.$(SRCEXT))

README

100644100755
File mode changed.

bin/htdocs/test.html

100644100755
File mode changed.

src/ByteBuffer.cpp

100644100755
File mode changed.

src/ByteBuffer.h

100644100755
File mode changed.

src/Client.cpp

100644100755
Lines changed: 34 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818

1919
#include "Client.h"
2020

21-
Client::Client(SOCKET fd, sockaddr_in addr) {
21+
Client::Client(int fd, sockaddr_in addr) {
2222
socketDesc = fd;
2323
clientAddr = addr;
2424
}
@@ -27,36 +27,56 @@ Client::~Client() {
2727
clearSendQueue();
2828
}
2929

30+
/**
31+
* Add to Send Queue
32+
* Adds a SendQueueItem object to the end of this client's send queue
33+
*/
3034
void Client::addToSendQueue(SendQueueItem* item) {
3135
sendQueue.push(item);
3236
}
3337

38+
/**
39+
* sendQueueSize
40+
* Returns the current number of SendQueueItem's in the send queue
41+
*
42+
* @return Integer representing number of items in this clients send queue
43+
*/
3444
unsigned int Client::sendQueueSize() {
3545
return sendQueue.size();
3646
}
3747

38-
SendQueueItem* Client::nextFromSendQueue(unsigned int bytesToSend) {
48+
/**
49+
* Next from Send Queue
50+
* Returns the current SendQueueItem object to be sent to the client
51+
*
52+
* @return SendQueueItem object containing the data to send and current offset
53+
*/
54+
SendQueueItem* Client::nextInSendQueue() {
3955
if(sendQueue.empty())
4056
return NULL;
4157

42-
SendQueueItem* item = sendQueue.front();
43-
unsigned int remaining = item->getSize() - item->getOffset();
44-
58+
return sendQueue.front();
59+
}
4560

46-
if(bytesToSend > remaining) { // Rest or entire resource data can be sent, Dequeue
47-
sendQueue.pop(); // Dequeue
48-
} else { // Only a portion of the resource can be sent, keep in queue
49-
// Add to the back of the queue
61+
/**
62+
* Dequeue from Send Queue
63+
* Deletes and dequeues first item in the queue
64+
*/
65+
void Client::dequeueFromSendQueue() {
66+
SendQueueItem* item = nextInSendQueue();
67+
if(item != NULL) {
5068
sendQueue.pop();
51-
sendQueue.push(item);
69+
delete item;
5270
}
53-
54-
return item;
5571
}
56-
72+
73+
/**
74+
* Clear Send Queue
75+
* Clears out the send queue for the client, deleting all internal SendQueueItem objects
76+
*/
5777
void Client::clearSendQueue() {
5878
while(!sendQueue.empty()) {
5979
delete sendQueue.front();
6080
sendQueue.pop();
6181
}
62-
}
82+
}

src/Client.h

100644100755
Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -25,25 +25,23 @@
2525

2626
#include "SendQueueItem.h"
2727

28-
#define SOCKET int
29-
3028
typedef unsigned char byte;
3129

3230
class Client {
33-
SOCKET socketDesc; // Socket Descriptor
31+
int socketDesc; // Socket Descriptor
3432
sockaddr_in clientAddr;
3533

3634
std::queue<SendQueueItem*> sendQueue;
3735

3836
public:
39-
Client(SOCKET fd, sockaddr_in addr);
37+
Client(int fd, sockaddr_in addr);
4038
~Client();
4139

4240
sockaddr_in getClientAddr() {
4341
return clientAddr;
4442
}
4543

46-
SOCKET getSocket() {
44+
int getSocket() {
4745
return socketDesc;
4846
}
4947

@@ -53,7 +51,8 @@ class Client {
5351

5452
void addToSendQueue(SendQueueItem* item);
5553
unsigned int sendQueueSize();
56-
SendQueueItem* nextFromSendQueue(unsigned int bytesToSend);
54+
SendQueueItem* nextInSendQueue();
55+
void dequeueFromSendQueue();
5756
void clearSendQueue();
5857
};
5958

src/HTTPMessage.cpp

100644100755
Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/**
2-
httpserver
2+
ByteBuffer
33
HTTPMessage.cpp
4-
Copyright 2011-2012 Ramsey Kant
4+
Copyright 2011-2014 Ramsey Kant
55
66
Licensed under the Apache License, Version 2.0 (the "License");
77
you may not use this file except in compliance with the License.
@@ -42,7 +42,7 @@ void HTTPMessage::init() {
4242
data = NULL;
4343
dataLen = 0;
4444

45-
version = HTTP_VERSION; // By default, all create() will indicate the version is whatever HTTP_VERSION is
45+
version = DEFAULT_HTTP_VERSION; // By default, all create() will indicate the version is whatever DEFAULT_HTTP_VERSION is
4646

4747
headers = new std::map<std::string, std::string>();
4848
}

src/HTTPMessage.h

100644100755
Lines changed: 19 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,20 @@
11
/**
2-
ByteBuffer
3-
HTTPMessage.h
4-
Copyright 2011 Ramsey Kant
5-
6-
Licensed under the Apache License, Version 2.0 (the "License");
7-
you may not use this file except in compliance with the License.
8-
You may obtain a copy of the License at
9-
10-
http://www.apache.org/licenses/LICENSE-2.0
11-
12-
Unless required by applicable law or agreed to in writing, software
13-
distributed under the License is distributed on an "AS IS" BASIS,
14-
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15-
See the License for the specific language governing permissions and
16-
limitations under the License.
17-
*/
2+
ByteBuffer
3+
HTTPMessage.h
4+
Copyright 2011-2014 Ramsey Kant
5+
6+
Licensed under the Apache License, Version 2.0 (the "License");
7+
you may not use this file except in compliance with the License.
8+
You may obtain a copy of the License at
9+
10+
http://www.apache.org/licenses/LICENSE-2.0
11+
12+
Unless required by applicable law or agreed to in writing, software
13+
distributed under the License is distributed on an "AS IS" BASIS,
14+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
See the License for the specific language governing permissions and
16+
limitations under the License.
17+
*/
1818

1919
#ifndef _HTTPMESSAGE_H_
2020
#define _HTTPMESSAGE_H_
@@ -26,7 +26,9 @@
2626
#include "ByteBuffer.h"
2727

2828
// Constants
29-
#define HTTP_VERSION "HTTP/1.1"
29+
#define HTTP_VERSION_10 "HTTP/1.0"
30+
#define HTTP_VERSION_11 "HTTP/1.1"
31+
#define DEFAULT_HTTP_VERSION "HTTP/1.1"
3032
#define NUM_METHODS 9
3133

3234
// HTTP Methods (Requests)

0 commit comments

Comments
 (0)