1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561
|
/*
GameSpy GHTTP SDK
Dan "Mr. Pants" Schoenblum
dan@gamespy.com
Copyright 1999-2007 GameSpy Industries, Inc
devsupport@gamespy.com
*/
#include "ghttpBuffer.h"
#include "ghttpConnection.h"
#include "ghttpMain.h"
#include "ghttpCommon.h"
#include "../common/gsCrypt.h"
#include "../common/gsSSL.h"
// Resize the buffer.
/////////////////////
GHTTPBool ghiResizeBuffer
(
GHIBuffer * buffer,
int sizeIncrement
)
{
char * tempPtr;
int newSize;
assert(buffer);
assert(sizeIncrement > 0);
assert(buffer->fixed == GHTTPFalse); // implied by sizeIncrement > 0
// Check args.
//////////////
if(!buffer)
return GHTTPFalse;
if(sizeIncrement <= 0)
return GHTTPFalse;
// Reallocate with the bigger size.
///////////////////////////////////
newSize = (buffer->size + sizeIncrement);
tempPtr = (char *)gsirealloc(buffer->data, (unsigned int)newSize);
if(!tempPtr)
return GHTTPFalse;
// Set the new info.
////////////////////
buffer->data = tempPtr;
buffer->size = newSize;
return GHTTPTrue;
}
GHTTPBool ghiInitBuffer
(
struct GHIConnection * connection,
GHIBuffer * buffer,
int initialSize,
int sizeIncrement
)
{
GHTTPBool bResult;
assert(connection);
assert(buffer);
assert(initialSize > 0);
assert(sizeIncrement > 0);
// Check args.
//////////////
if(!connection)
return GHTTPFalse;
if(!buffer)
return GHTTPFalse;
if(initialSize <= 0)
return GHTTPFalse;
if(sizeIncrement <= 0)
return GHTTPFalse;
// Init the struct.
///////////////////
buffer->connection = connection;
buffer->data = NULL;
buffer->size = 0;
buffer->len = 0;
buffer->pos = 0;
buffer->sizeIncrement = sizeIncrement;
buffer->fixed = GHTTPFalse;
buffer->dontFree = GHTTPFalse;
buffer->readOnly = GHTTPFalse;
// Do the initial resize.
/////////////////////////
bResult = ghiResizeBuffer(buffer, initialSize);
if(!bResult)
return GHTTPFalse;
// Start with an empty string.
//////////////////////////////
*buffer->data = '\0';
return GHTTPTrue;
}
GHTTPBool ghiInitFixedBuffer
(
struct GHIConnection * connection,
GHIBuffer * buffer,
char * userBuffer,
int size
)
{
assert(connection);
assert(buffer);
assert(userBuffer);
assert(size > 0);
// Check args.
//////////////
if(!connection)
return GHTTPFalse;
if(!buffer)
return GHTTPFalse;
if(!userBuffer)
return GHTTPFalse;
if(size <= 0)
return GHTTPFalse;
// Init the struct.
///////////////////
buffer->connection = connection;
buffer->data = userBuffer;
buffer->size = size;
buffer->len = 0;
buffer->pos = 0;
buffer->sizeIncrement = 0;
buffer->fixed = GHTTPTrue;
buffer->dontFree = GHTTPTrue;
buffer->readOnly = GHTTPFalse;
// Start with an empty string.
//////////////////////////////
*buffer->data = '\0';
return GHTTPTrue;
}
GHTTPBool ghiInitReadOnlyBuffer
(
struct GHIConnection * connection, // The connection.
GHIBuffer * buffer, // The buffer to init.
const char * userBuffer, // The user-buffer to use.
int size // The size of the buffer.
)
{
assert(connection);
assert(buffer);
assert(userBuffer);
assert(size > 0);
// Check args.
//////////////
if(!connection)
return GHTTPFalse;
if(!buffer)
return GHTTPFalse;
if(!userBuffer)
return GHTTPFalse;
if(size <= 0)
return GHTTPFalse;
// Init the struct.
///////////////////
buffer->connection = connection;
buffer->data = (char*)userBuffer; // cast away const
buffer->size = size;
buffer->pos = 0;
buffer->sizeIncrement = 0;
buffer->fixed = GHTTPTrue;
buffer->dontFree = GHTTPTrue;
buffer->readOnly = GHTTPTrue;
// Start with user supplied data
//////////////////////////////
buffer->len = size;
return GHTTPTrue;
}
void ghiFreeBuffer
(
GHIBuffer * buffer
)
{
assert(buffer);
// Check args.
//////////////
if(!buffer)
return;
if(!buffer->data)
return;
// Cleanup the struct.
//////////////////////
if(!buffer->dontFree)
gsifree(buffer->data);
memset(buffer, 0, sizeof(GHIBuffer));
}
GHTTPBool ghiAppendDataToBuffer
(
GHIBuffer * buffer,
const char * data,
int dataLen
)
{
GHTTPBool bResult;
int newLen;
assert(buffer);
assert(data);
assert(dataLen >= 0);
// Check args.
//////////////
if(!buffer)
return GHTTPFalse;
if(!data)
return GHTTPFalse;
if(dataLen < 0)
return GHTTPFalse;
if (buffer->readOnly)
return GHTTPFalse;
// Get the string length if needed.
///////////////////////////////////
if(dataLen == 0)
dataLen = (int)strlen(data);
// Get the new length.
//////////////////////
newLen = (buffer->len + dataLen);
// Make sure the array is big enough.
/////////////////////////////////////
while(newLen >= buffer->size)
{
// Check for a fixed buffer.
////////////////////////////
if(buffer->fixed)
{
buffer->connection->completed = GHTTPTrue;
buffer->connection->result = GHTTPBufferOverflow;
return GHTTPFalse;
}
bResult = ghiResizeBuffer(buffer, buffer->sizeIncrement);
if(!bResult)
{
buffer->connection->completed = GHTTPTrue;
buffer->connection->result = GHTTPOutOfMemory;
return GHTTPFalse;
}
}
// Add the data.
////////////////
memcpy(buffer->data + buffer->len, data, (unsigned int)dataLen);
buffer->len = newLen;
buffer->data[buffer->len] = '\0';
return GHTTPTrue;
}
// Use sparingly. This function wraps the data in an SSL record.
GHTTPBool ghiEncryptDataToBuffer
(
GHIBuffer * buffer,
const char * data,
int dataLen
)
{
GHIEncryptionResult result;
int bufSpace = 0;
int pos = 0;
assert(buffer);
assert(data);
assert(dataLen >= 0);
// Check args.
//////////////
if(!buffer)
return GHTTPFalse;
if(!data)
return GHTTPFalse;
if(dataLen < 0)
return GHTTPFalse;
if (buffer->readOnly)
return GHTTPFalse;
// Switch to plain text append when not using SSL
if (buffer->connection->encryptor.mEngine == GHTTPEncryptionEngine_None ||
buffer->connection->encryptor.mSessionEstablished == GHTTPFalse)
{
return ghiAppendDataToBuffer(buffer, data, dataLen);
}
// Get the string length if needed.
///////////////////////////////////
if(dataLen == 0)
dataLen = (int)strlen(data);
if (dataLen == 0)
return GHTTPTrue; // no data and strlen == 0
bufSpace = buffer->size - buffer->len;
do
{
int fragmentLen = min(dataLen, GS_SSL_MAX_CONTENTLENGTH);
// Call the encryptor function
// bufSize is reduced by the number of bytes written
result = buffer->connection->encryptor.mEncryptFunc(buffer->connection, &buffer->connection->encryptor,
&data[pos], dataLen,
&buffer->data[buffer->len], &bufSpace);
if (result == GHIEncryptionResult_BufferTooSmall)
{
if (ghiResizeBuffer(buffer, buffer->sizeIncrement) == GHTTPFalse)
return GHTTPFalse;
bufSpace = buffer->size - buffer->len;
}
else if (result == GHIEncryptionResult_Success)
{
// update data and buffer positions
pos += fragmentLen;
buffer->len = buffer->size - bufSpace;
}
else
{
gsDebugFormat(GSIDebugCat_HTTP, GSIDebugType_Misc, GSIDebugLevel_WarmError,
"ghiEncryptDataToBuffer encountered unhandled return code: %d\r\n", result);
return GHTTPFalse;
}
} while(pos < dataLen);
return GHTTPTrue;
}
GHTTPBool ghiAppendHeaderToBuffer
(
GHIBuffer * buffer,
const char * name,
const char * value
)
{
if(!ghiAppendDataToBuffer(buffer, name, 0))
return GHTTPFalse;
if(!ghiAppendDataToBuffer(buffer, ": ", 2))
return GHTTPFalse;
if(!ghiAppendDataToBuffer(buffer, value, 0))
return GHTTPFalse;
if(!ghiAppendDataToBuffer(buffer, CRLF, 2))
return GHTTPFalse;
return GHTTPTrue;
}
GHTTPBool ghiAppendCharToBuffer
(
GHIBuffer * buffer,
int c
)
{
GHTTPBool bResult;
assert(buffer);
// Check args.
//////////////
if(!buffer)
return GHTTPFalse;
if (buffer->readOnly)
return GHTTPFalse;
// Make sure the array is big enough.
/////////////////////////////////////
if((buffer->len + 1) >= buffer->size)
{
// Check for a fixed buffer.
////////////////////////////
if(buffer->fixed)
{
buffer->connection->completed = GHTTPTrue;
buffer->connection->result = GHTTPBufferOverflow;
return GHTTPFalse;
}
bResult = ghiResizeBuffer(buffer, buffer->sizeIncrement);
if(!bResult)
{
buffer->connection->completed = GHTTPTrue;
buffer->connection->result = GHTTPOutOfMemory;
return GHTTPFalse;
}
}
// Add the char.
////////////////
buffer->data[buffer->len] = (char)(c & 0xFF);
buffer->len++;
buffer->data[buffer->len] = '\0';
return GHTTPTrue;
}
GHTTPBool ghiAppendIntToBuffer
(
GHIBuffer * buffer,
int i
)
{
char intValue[16];
sprintf(intValue, "%d", i);
return ghiAppendDataToBuffer(buffer, intValue, 0);
}
void ghiResetBuffer
(
GHIBuffer * buffer
)
{
assert(buffer);
buffer->len = 0;
buffer->pos = 0;
// Start with an empty string.
//////////////////////////////
if (!buffer->readOnly)
*buffer->data = '\0';
}
GHTTPBool ghiSendBufferedData
(
struct GHIConnection * connection
)
{
int rcode;
int writeFlag;
int exceptFlag;
char * data;
int len;
// Loop while we can send.
//////////////////////////
do
{
rcode = GSISocketSelect(connection->socket, NULL, &writeFlag, &exceptFlag);
if((gsiSocketIsError(rcode)) || ((rcode == 1) && exceptFlag))
{
connection->completed = GHTTPTrue;
connection->result = GHTTPSocketFailed;
if(gsiSocketIsError(rcode))
connection->socketError = GOAGetLastError(connection->socket);
else
connection->socketError = 0;
return GHTTPFalse;
}
if((rcode < 1) || !writeFlag)
{
// Can't send anything.
///////////////////////
return GHTTPTrue;
}
// Figure out what, and how much, to send.
//////////////////////////////////////////
data = (connection->sendBuffer.data + connection->sendBuffer.pos);
len = (connection->sendBuffer.len - connection->sendBuffer.pos);
// Do the send.
///////////////
rcode = ghiDoSend(connection, data, len);
if(gsiSocketIsError(rcode))
return GHTTPFalse;
// Update the position.
///////////////////////
connection->sendBuffer.pos += rcode;
}
while(connection->sendBuffer.pos < connection->sendBuffer.len);
return GHTTPTrue;
}
// Read data from a buffer
GHTTPBool ghiReadDataFromBuffer
(
GHIBuffer * bufferIn, // the GHIBuffer to read from
char bufferOut[], // the raw buffer to write to
int * len // max number of bytes to append, becomes actual length written
)
{
int bytesAvailable = 0;
int bytesToCopy = 0;
// Verify parameters
assert(bufferIn != NULL);
assert(len != NULL);
if (*len == 0)
return GHTTPFalse;
// Make sure the bufferIn isn't emtpy
bytesAvailable = (int)bufferIn->len - bufferIn->pos;
if (bytesAvailable <= 0)
return GHTTPFalse;
// Calculate the actual number of bytes to copy
bytesToCopy = min(*len-1, bytesAvailable);
// Copy the bytes
memcpy(bufferOut, bufferIn->data + bufferIn->pos, (size_t)bytesToCopy);
bufferOut[bytesToCopy] = '\0';
*len = bytesToCopy;
// Adjust the bufferIn read position
bufferIn->pos += bytesToCopy;
return GHTTPTrue;
}
// Read data from a buffer with a garunteed length
GHTTPBool ghiReadDataFromBufferFixed
(
GHIBuffer * bufferIn, // the GHIBuffer to read from
char bufferOut[], // the raw buffer to write to
int bytesToCopy // number of bytes to read
)
{
// Verify parameters
assert(bufferIn != NULL);
if (bytesToCopy == 0)
return GHTTPTrue;
// Make sure the bufferIn isn't too small
if (bufferIn->len < bytesToCopy)
return GHTTPFalse;
// Copy the bytes
memcpy(bufferOut, bufferIn->data + bufferIn->pos, (size_t)bytesToCopy);
// Adjust the bufferIn read position
bufferIn->pos += bytesToCopy;
return GHTTPTrue;
}
|