Bug 945533 - Convert various spdy code to use mozilla/Endian.h rather than NSPR endianness conversion functions. r=mcmanus

--HG--
extra : rebase_source : 11b47056c1e14e0751c351ab199609e07e15399f
This commit is contained in:
Jeff Walden 2013-05-03 15:51:35 -07:00
parent 71e5708f15
commit 9154d14697
2 changed files with 45 additions and 53 deletions

View File

@ -7,12 +7,12 @@
// HttpLog.h should generally be included first
#include "HttpLog.h"
#include "mozilla/Endian.h"
#include "mozilla/Telemetry.h"
#include "nsHttp.h"
#include "nsHttpHandler.h"
#include "nsILoadGroup.h"
#include "prprf.h"
#include "prnetdb.h"
#include "SpdyPush3.h"
#include "SpdySession3.h"
#include "SpdyStream3.h"
@ -652,8 +652,7 @@ SpdySession3::GeneratePing(uint32_t aID)
packet[6] = 0;
packet[7] = 4; /* length */
aID = PR_htonl(aID);
memcpy(packet + 8, &aID, 4);
NetworkEndian::writeUint32(packet + 8, aID);
LogIO(this, nullptr, "Generate Ping", packet, 12);
FlushOutputQueue();
@ -679,10 +678,8 @@ SpdySession3::GenerateRstStream(uint32_t aStatusCode, uint32_t aID)
packet[6] = 0;
packet[7] = 8; /* length */
aID = PR_htonl(aID);
memcpy(packet + 8, &aID, 4);
aStatusCode = PR_htonl(aStatusCode);
memcpy(packet + 12, &aStatusCode, 4);
NetworkEndian::writeUint32(packet + 8, aID);
NetworkEndian::writeUint32(packet + 12, aStatusCode);
LogIO(this, nullptr, "Generate Reset", packet, 16);
FlushOutputQueue();
@ -709,8 +706,7 @@ SpdySession3::GenerateGoAway(uint32_t aStatusCode)
// need to be set non zero
// bytes 12-15 are the status code.
aStatusCode = PR_htonl(aStatusCode);
memcpy(packet + 12, &aStatusCode, 4);
NetworkEndian::writeUint32(packet + 12, aStatusCode);
LogIO(this, nullptr, "Generate GoAway", packet, 16);
FlushOutputQueue();
@ -756,8 +752,7 @@ SpdySession3::GenerateSettings()
packet[12 + 8 * numberOfEntries] = PERSISTED_VALUE;
packet[15 + 8 * numberOfEntries] = SETTINGS_TYPE_CWND;
LOG(("SpdySession3::GenerateSettings %p sending CWND %u\n", this, cwnd));
cwnd = PR_htonl(cwnd);
memcpy(packet + 16 + 8 * numberOfEntries, &cwnd, 4);
NetworkEndian::writeUint32(packet + 16 + 8 * numberOfEntries, cwnd);
numberOfEntries++;
}
@ -765,8 +760,7 @@ SpdySession3::GenerateSettings()
// a window update with it in order to use larger initial windows with pulled
// streams.
packet[15 + 8 * numberOfEntries] = SETTINGS_TYPE_INITIAL_WINDOW;
uint32_t rwin = PR_htonl(mPushAllowance);
memcpy(packet + 16 + 8 * numberOfEntries, &rwin, 4);
NetworkEndian::writeUint32(packet + 16 + 8 * numberOfEntries, mPushAllowance);
numberOfEntries++;
uint32_t dataLen = 4 + 8 * numberOfEntries;
@ -943,9 +937,9 @@ SpdySession3::HandleSynStream(SpdySession3 *self)
}
uint32_t streamID =
PR_ntohl(reinterpret_cast<uint32_t *>(self->mInputFrameBuffer.get())[2]);
NetworkEndian::readUint32(self->mInputFrameBuffer + 2 * sizeof(uint32_t));
uint32_t associatedID =
PR_ntohl(reinterpret_cast<uint32_t *>(self->mInputFrameBuffer.get())[3]);
NetworkEndian::readUint32(self->mInputFrameBuffer + 3 * sizeof(uint32_t));
uint8_t flags = reinterpret_cast<uint8_t *>(self->mInputFrameBuffer.get())[4];
LOG3(("SpdySession3::HandleSynStream %p recv SYN_STREAM (push) "
@ -1124,7 +1118,7 @@ SpdySession3::HandleSynReply(SpdySession3 *self)
LOG3(("SpdySession3::HandleSynReply %p lookup via streamID in syn_reply.\n",
self));
uint32_t streamID =
PR_ntohl(reinterpret_cast<uint32_t *>(self->mInputFrameBuffer.get())[2]);
NetworkEndian::readUint32(self->mInputFrameBuffer + 2 * sizeof(uint32_t));
nsresult rv = self->SetInputFrameDataStream(streamID);
if (NS_FAILED(rv))
return rv;
@ -1256,10 +1250,10 @@ SpdySession3::HandleRstStream(SpdySession3 *self)
uint8_t flags = reinterpret_cast<uint8_t *>(self->mInputFrameBuffer.get())[4];
uint32_t streamID =
PR_ntohl(reinterpret_cast<uint32_t *>(self->mInputFrameBuffer.get())[2]);
NetworkEndian::readUint32(self->mInputFrameBuffer + 2 * sizeof(uint32_t));
self->mDownstreamRstReason =
PR_ntohl(reinterpret_cast<uint32_t *>(self->mInputFrameBuffer.get())[3]);
NetworkEndian::readUint32(self->mInputFrameBuffer + 3 * sizeof(uint32_t));
LOG3(("SpdySession3::HandleRstStream %p RST_STREAM Reason Code %u ID %x "
"flags %x", self, self->mDownstreamRstReason, streamID, flags));
@ -1319,7 +1313,7 @@ SpdySession3::HandleSettings(SpdySession3 *self)
}
uint32_t numEntries =
PR_ntohl(reinterpret_cast<uint32_t *>(self->mInputFrameBuffer.get())[2]);
NetworkEndian::readUint32(self->mInputFrameBuffer + 2 * sizeof(uint32_t));
// Ensure frame is large enough for supplied number of entries
// Each entry is 8 bytes, frame data is reduced by 4 to account for
@ -1338,8 +1332,8 @@ SpdySession3::HandleSettings(SpdySession3 *self)
(self->mInputFrameBuffer.get()) + 12 + index * 8;
uint32_t flags = setting[0];
uint32_t id = PR_ntohl(reinterpret_cast<uint32_t *>(setting)[0]) & 0xffffff;
uint32_t value = PR_ntohl(reinterpret_cast<uint32_t *>(setting)[1]);
uint32_t id = NetworkEndian::readUint32(setting) & 0xffffff;
uint32_t value = NetworkEndian::readUint32(setting + 1 * sizeof(uint32_t));
LOG3(("Settings ID %d, Flags %X, Value %d", id, flags, value));
@ -1425,7 +1419,7 @@ SpdySession3::HandlePing(SpdySession3 *self)
}
uint32_t pingID =
PR_ntohl(reinterpret_cast<uint32_t *>(self->mInputFrameBuffer.get())[2]);
NetworkEndian::readUint32(self->mInputFrameBuffer + 2 * sizeof(uint32_t));
LOG3(("SpdySession3::HandlePing %p PING ID 0x%X.", self, pingID));
@ -1455,7 +1449,7 @@ SpdySession3::HandleGoAway(SpdySession3 *self)
self->mShouldGoAway = true;
self->mGoAwayID =
PR_ntohl(reinterpret_cast<uint32_t *>(self->mInputFrameBuffer.get())[2]);
NetworkEndian::readUint32(self->mInputFrameBuffer + 2 * sizeof(uint32_t));
self->mCleanShutdown = true;
// Find streams greater than the last-good ID and mark them for deletion
@ -1488,7 +1482,8 @@ SpdySession3::HandleGoAway(SpdySession3 *self)
LOG3(("SpdySession3::HandleGoAway %p GOAWAY Last-Good-ID 0x%X status 0x%X "
"live streams=%d\n", self, self->mGoAwayID,
PR_ntohl(reinterpret_cast<uint32_t *>(self->mInputFrameBuffer.get())[3]),
NetworkEndian::readUint32(self->mInputFrameBuffer +
3 * sizeof(uint32_t)),
self->mStreamTransactionHash.Count()));
self->ResetDownstreamState();
@ -1507,7 +1502,7 @@ SpdySession3::HandleHeaders(SpdySession3 *self)
}
uint32_t streamID =
PR_ntohl(reinterpret_cast<uint32_t *>(self->mInputFrameBuffer.get())[2]);
NetworkEndian::readUint32(self->mInputFrameBuffer + 2 * sizeof(uint32_t));
LOG3(("SpdySession3::HandleHeaders %p HEADERS for Stream 0x%X.\n",
self, streamID));
nsresult rv = self->SetInputFrameDataStream(streamID);
@ -1585,10 +1580,10 @@ SpdySession3::HandleWindowUpdate(SpdySession3 *self)
}
uint32_t delta =
PR_ntohl(reinterpret_cast<uint32_t *>(self->mInputFrameBuffer.get())[3]);
NetworkEndian::readUint32(self->mInputFrameBuffer + 3 * sizeof(uint32_t));
delta &= 0x7fffffff;
uint32_t streamID =
PR_ntohl(reinterpret_cast<uint32_t *>(self->mInputFrameBuffer.get())[2]);
NetworkEndian::readUint32(self->mInputFrameBuffer + 2 * sizeof(uint32_t));
streamID &= 0x7fffffff;
LOG3(("SpdySession3::HandleWindowUpdate %p len=%d for Stream 0x%X.\n",
@ -1898,7 +1893,7 @@ SpdySession3::WriteSegments(nsAHttpSegmentWriter *writer,
// For both control and data frames the second 32 bit word of the header
// is 8-flags, 24-length. (network byte order)
mInputFrameDataSize =
PR_ntohl(reinterpret_cast<uint32_t *>(mInputFrameBuffer.get())[1]);
NetworkEndian::readUint32(mInputFrameBuffer + 1 * sizeof(uint32_t));
mInputFrameDataSize &= 0x00ffffff;
mInputFrameDataRead = 0;
@ -1909,12 +1904,11 @@ SpdySession3::WriteSegments(nsAHttpSegmentWriter *writer,
// The first 32 bit word of the header is
// 1 ctrl - 15 version - 16 type
uint16_t version =
PR_ntohs(reinterpret_cast<uint16_t *>(mInputFrameBuffer.get())[0]);
uint16_t version = NetworkEndian::readUint16(mInputFrameBuffer);
version &= 0x7fff;
mFrameControlType =
PR_ntohs(reinterpret_cast<uint16_t *>(mInputFrameBuffer.get())[1]);
NetworkEndian::readUint16(mInputFrameBuffer + sizeof(uint16_t));
LOG3(("SpdySession3::WriteSegments %p - Control Frame Identified "
"type %d version %d data len %d",
@ -1934,8 +1928,7 @@ SpdySession3::WriteSegments(nsAHttpSegmentWriter *writer,
mInputFrameDataSize >> 10);
mLastDataReadEpoch = mLastReadEpoch;
uint32_t streamID =
PR_ntohl(reinterpret_cast<uint32_t *>(mInputFrameBuffer.get())[0]);
uint32_t streamID = NetworkEndian::readUint32(mInputFrameBuffer);
rv = SetInputFrameDataStream(streamID);
if (NS_FAILED(rv)) {
LOG(("SpdySession3::WriteSegments %p lookup streamID 0x%X failed. "
@ -2193,10 +2186,8 @@ SpdySession3::UpdateLocalRwin(SpdyStream3 *stream,
packet[3] = CONTROL_TYPE_WINDOW_UPDATE;
packet[7] = dataLen;
uint32_t id = PR_htonl(stream->StreamID());
memcpy(packet + 8, &id, 4);
toack = PR_htonl(toack);
memcpy(packet + 12, &toack, 4);
NetworkEndian::writeUint32(packet + 8, stream->StreamID());
NetworkEndian::writeUint32(packet + 12, toack);
LogIO(this, stream, "Window Update", packet, 8 + dataLen);
FlushOutputQueue();

View File

@ -7,6 +7,7 @@
// HttpLog.h should generally be included first
#include "HttpLog.h"
#include "mozilla/Endian.h"
#include "mozilla/Telemetry.h"
#include "nsHttp.h"
#include "nsHttpHandler.h"
@ -338,12 +339,11 @@ SpdyStream3::ParseHttpRequestHeaders(const char *buf,
mTxInlineFrame[3] = SpdySession3::CONTROL_TYPE_SYN_STREAM;
// 4 to 7 are length and flags, we'll fill that in later
uint32_t networkOrderID = PR_htonl(mStreamID);
memcpy(mTxInlineFrame + 8, &networkOrderID, 4);
NetworkEndian::writeUint32(mTxInlineFrame + 8, mStreamID);
// this is the associated-to field, which is not used sending
// from the client in the http binding
memset (mTxInlineFrame + 12, 0, 4);
memset(mTxInlineFrame + 12, 0, 4);
// Priority flags are the E0 mask of byte 16.
// 0 is highest priority, 7 is lowest.
@ -469,8 +469,8 @@ SpdyStream3::ParseHttpRequestHeaders(const char *buf,
CompressFlushFrame();
// 4 to 7 are length and flags, which we can now fill in
(reinterpret_cast<uint32_t *>(mTxInlineFrame.get()))[1] =
PR_htonl(mTxInlineFrameUsed - 8);
NetworkEndian::writeUint32(mTxInlineFrame + 1 * sizeof(uint32_t),
mTxInlineFrameUsed - 8);
MOZ_ASSERT(!mTxInlineFrame[4], "Size greater than 24 bits");
@ -768,9 +768,8 @@ SpdyStream3::GenerateDataFrameHeader(uint32_t dataLength, bool lastFrame)
MOZ_ASSERT(!mTxStreamFrameSize, "stream frame not empty");
MOZ_ASSERT(!(dataLength & 0xff000000), "datalength > 24 bits");
(reinterpret_cast<uint32_t *>(mTxInlineFrame.get()))[0] = PR_htonl(mStreamID);
(reinterpret_cast<uint32_t *>(mTxInlineFrame.get()))[1] =
PR_htonl(dataLength);
NetworkEndian::writeUint32(mTxInlineFrame, mStreamID);
NetworkEndian::writeUint32(mTxInlineFrame + 1 * sizeof(uint32_t), dataLength);
MOZ_ASSERT(!(mTxInlineFrame[0] & 0x80), "control bit set unexpectedly");
MOZ_ASSERT(!mTxInlineFrame[4], "flag bits set unexpectedly");
@ -1065,7 +1064,7 @@ SpdyStream3::FindHeader(nsCString name,
return NS_ERROR_ILLEGAL_VALUE;
do {
uint32_t numPairs = PR_ntohl(reinterpret_cast<const uint32_t *>(nvpair)[-1]);
uint32_t numPairs = NetworkEndian::readUint32(nvpair - 1 * sizeof(uint32_t));
for (uint32_t index = 0; index < numPairs; ++index) {
if (lastHeaderByte < nvpair + 4)
@ -1147,7 +1146,7 @@ SpdyStream3::ConvertHeaders(nsACString &aHeadersOut)
return NS_ERROR_ILLEGAL_VALUE;
do {
uint32_t numPairs = PR_ntohl(reinterpret_cast<const uint32_t *>(nvpair)[-1]);
uint32_t numPairs = NetworkEndian::readUint32(nvpair - 1 * sizeof(uint32_t));
for (uint32_t index = 0; index < numPairs; ++index) {
if (lastHeaderByte < nvpair + 4)
@ -1285,10 +1284,11 @@ SpdyStream3::CompressToFrame(uint32_t data)
{
// convert the data to 4 byte network byte order and write that
// to the compressed stream
data = PR_htonl(data);
unsigned char databuf[sizeof(data)];
NetworkEndian::writeUint32(databuf, data);
mZlib->next_in = reinterpret_cast<unsigned char *> (&data);
mZlib->avail_in = 4;
mZlib->next_in = databuf;
mZlib->avail_in = sizeof(databuf);
ExecuteCompress(Z_NO_FLUSH);
}
@ -1299,11 +1299,12 @@ SpdyStream3::CompressToFrame(const char *data, uint32_t len)
// Format calls for a network ordered 32 bit length
// followed by the utf8 string
uint32_t networkLen = PR_htonl(len);
unsigned char lenbuf[sizeof(len)];
NetworkEndian::writeUint32(lenbuf, len);
// write out the length
mZlib->next_in = reinterpret_cast<unsigned char *> (&networkLen);
mZlib->avail_in = 4;
mZlib->next_in = lenbuf;
mZlib->avail_in = sizeof(lenbuf);
ExecuteCompress(Z_NO_FLUSH);
// write out the data