headers.cpp 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. #include "foundation/error.h"
  2. #include "foundation/types.h"
  3. #include "headers.h"
  4. #include "netinc.h"
  5. #include <stdlib.h>
  6. #include <string.h>
  7. JNL_Headers::JNL_Headers()
  8. {
  9. m_recvheaders = NULL;
  10. m_recvheaders_size = 0;
  11. }
  12. JNL_Headers::~JNL_Headers()
  13. {
  14. if ( m_recvheaders )
  15. free( m_recvheaders );
  16. }
  17. void JNL_Headers::Reset()
  18. {
  19. if ( m_recvheaders )
  20. free( m_recvheaders );
  21. m_recvheaders = NULL;
  22. m_recvheaders_size = 0;
  23. }
  24. const char *JNL_Headers::GetAllHeaders()
  25. {
  26. // double null terminated, null delimited list
  27. if ( m_recvheaders )
  28. return m_recvheaders;
  29. else
  30. return "\0\0";
  31. }
  32. const char *JNL_Headers::GetHeader( const char *headername )
  33. {
  34. char *ret = NULL;
  35. if ( headername[ 0 ] == 0 || !m_recvheaders )
  36. return NULL;
  37. size_t headername_size = strlen( headername );
  38. char *buf = (char *)malloc( headername_size + 2 );
  39. strcpy( buf, headername );
  40. if ( buf[ headername_size - 1 ] != ':' )
  41. {
  42. buf[ headername_size++ ] = ':';
  43. buf[ headername_size ] = 0;
  44. }
  45. char *p = m_recvheaders;
  46. while ( p && *p )
  47. {
  48. if ( !strncasecmp( buf, p, headername_size ) )
  49. {
  50. ret = p + headername_size;
  51. while ( ret && *ret && *ret == ' ' )
  52. ret++;
  53. break;
  54. }
  55. p += strlen( p ) + 1;
  56. }
  57. free( buf );
  58. return ret;
  59. }
  60. int JNL_Headers::Add( const char *buf )
  61. {
  62. if ( !m_recvheaders )
  63. {
  64. m_recvheaders_size = strlen( buf ) + 1;
  65. if ( m_recvheaders_size == 0 || m_recvheaders_size == (size_t)-1 ) // check for overflow
  66. {
  67. return NErr_OutOfMemory;
  68. }
  69. m_recvheaders = (char *)malloc( m_recvheaders_size + 1 );
  70. if ( m_recvheaders )
  71. {
  72. strcpy( m_recvheaders, buf ); // safe because we malloc'd specifically above
  73. m_recvheaders[ m_recvheaders_size ] = 0;
  74. }
  75. else
  76. {
  77. return NErr_OutOfMemory;
  78. }
  79. }
  80. else
  81. {
  82. size_t oldsize = m_recvheaders_size;
  83. m_recvheaders_size += strlen( buf ) + 1;
  84. if ( m_recvheaders_size + 1 < oldsize ) // check for overflow
  85. {
  86. return NErr_OutOfMemory;
  87. }
  88. char *n = (char *)realloc( m_recvheaders, m_recvheaders_size + 1 );
  89. if ( !n )
  90. {
  91. return NErr_OutOfMemory;
  92. }
  93. strcpy( n + oldsize, buf ); // safe because we malloc specifially for the size
  94. n[ m_recvheaders_size ] = 0; // double null terminate
  95. m_recvheaders = n;
  96. }
  97. return NErr_Success;
  98. }