1010#endif /* HAVE_CONFIG_H */
1111
1212#include " args.h"
13+ #include " buffer.h"
1314#include " commparty.h"
1415#include " print.h"
1516#include " tcp.h"
@@ -47,11 +48,11 @@ int isIncomingIpPacket(const RawIpPacket* ip) {
4748
4849#ifdef ENABLE_JSON
4950
50- bool parseAndPrintJson (const char * data, uint16_t len ) {
51+ bool parseAndPrintJson (const Buffer& buffer ) {
5152 GError* error = NULL ;
5253 bool result = false ;
5354 JsonParser* parser = json_parser_new ();
54- json_parser_load_from_data (parser, data, len , &error);
55+ json_parser_load_from_data (parser, buffer. getData (), buffer. getLength () , &error);
5556
5657 if (error) {
5758 g_error_free (error);
@@ -93,8 +94,9 @@ void printTimestamp(struct timeval tv) {
9394 printf (" %02d:%02d:%02d.%06ld " , hours, minutes, seconds, microSeconds);
9495}
9596
96- void printHttpRequestTitle (const char * data, int /* len */ ) {
97+ void printHttpRequestTitle (const Buffer& buffer ) {
9798 printf (" ht " );
99+ const char * data = buffer.getData ();
98100 const char * eol_char = strchr (data, ' \r ' );
99101
100102 if (!eol_char) {
@@ -107,13 +109,16 @@ void printHttpRequestTitle(const char* data, int /* len */) {
107109 printf (" \n " );
108110}
109111
110- void handleHttpRequest (const char * data, int len) {
112+ void handleHttpRequest (const Buffer& buffer) {
113+ const char * data = buffer.getData ();
114+ int len = buffer.getLength ();
115+
111116 if (len > 10 && strncmp (data, " GET " , 4 ) == 0 ) {
112- printHttpRequestTitle (data, len );
117+ printHttpRequestTitle (buffer );
113118 } else if (len > 10 && strncmp (data, " POST " , 5 ) == 0 ) {
114- printHttpRequestTitle (data, len );
119+ printHttpRequestTitle (buffer );
115120 } else if (len > 10 && strncmp (data, " PUT " , 4 ) == 0 ) {
116- printHttpRequestTitle (data, len );
121+ printHttpRequestTitle (buffer );
117122 } else {
118123 printf (" ht DATA\n " );
119124 }
@@ -122,11 +127,11 @@ void handleHttpRequest(const char* data, int len) {
122127 printf (" \n " );
123128
124129#ifdef ENABLE_JSON
125- if (!parseAndPrintJson (data, len )) {
130+ if (!parseAndPrintJson (buffer )) {
126131#endif /* ENABLE_JSON */
127- printIndented (4 , data, len );
132+ printIndented (4 , buffer );
128133
129- if (data [len - 1 ] != ' \n ' ) {
134+ if (buffer [len - 1 ] != ' \n ' ) {
130135 printf (" \n " );
131136 }
132137#ifdef ENABLE_JSON
@@ -137,15 +142,17 @@ void handleHttpRequest(const char* data, int len) {
137142 }
138143}
139144
140- void handleHttpResponse (const char * data, int len ) {
141- if (len > 10 && strncmp (data, " HTTP/1.1" , 8 ) == 0 ) {
142- printHttpRequestTitle (data, len );
145+ void handleHttpResponse (const Buffer& buffer ) {
146+ if (buffer. startsWith ( " HTTP/1.1" ) ) {
147+ printHttpRequestTitle (buffer );
143148 } else {
144149 printf (" DATA\n " );
145150 }
146151
147152 if (!sArgs .useShortOutputFormat ()) {
148153 printf (" \n " );
154+ const char * data = buffer.getData ();
155+ int len = buffer.getLength ();
149156 const char * bodySeparator = strstr (data, " \r\n\r\n " );
150157
151158 if (bodySeparator) {
@@ -155,11 +162,12 @@ void handleHttpResponse(const char* data, int len) {
155162
156163 if (bodyLength > 0 ) {
157164 const char * body = bodySeparator + 4 ;
165+ Buffer buffer (body, bodyLength);
158166
159167#ifdef ENABLE_JSON
160- if (!parseAndPrintJson (body, bodyLength )) {
168+ if (!parseAndPrintJson (buffer )) {
161169#endif /* ENABLE_JSON */
162- printIndented (4 , body, bodyLength );
170+ printIndented (4 , buffer );
163171#ifdef ENABLE_JSON
164172 }
165173#endif /* ENABLE_JSON */
@@ -179,36 +187,38 @@ void printPacketInfo(string partyName, bool isIncoming, struct timeval tv) {
179187 printTimestamp (tv);
180188}
181189
182- void handleWebsocketNotification (string partyName, bool isIncoming, struct timeval tv, WebSocketParser* ws, const char * data, uint16_t len ) {
190+ void handleWebsocketNotification (string partyName, bool isIncoming, struct timeval tv, WebSocketParser* ws, const Buffer& data) {
183191 WebSocketFrame* frame;
184- ws->addStreamData (data, len );
192+ ws->addStreamData (data);
185193
186194 while ((frame = ws->getNextFrame ()) != nullptr ) {
187195 printPacketInfo (partyName, isIncoming, tv);
188196 printf (" ws %s\n " , frame->getSubject ().c_str ());
189197
190198 if (!sArgs .useShortOutputFormat ()) {
199+ Buffer frameData = frame->getData ();
200+
191201 if (frame->getType () == TEXT) {
192202 printf (" \n " );
193203
194- if (frame-> getDataLength () > 0 ) {
204+ if (frameData. getLength () > 0 ) {
195205#ifdef ENABLE_JSON
196- if (!parseAndPrintJson (frame-> getData (), frame-> getDataLength () )) {
206+ if (!parseAndPrintJson (frameData )) {
197207 printf (" " );
198- PRINT_BUFFER (frame-> getData (), frame-> getDataLength () );
208+ PRINT_BUFFER_1 (frameData );
199209 printf (" (FAILED TO PARSE)\n " );
200210 }
201211#else /* ENABLE_JSON */
202212 printf (" " );
203- PRINT_BUFFER (frame-> getData (), frame-> getDataLength () );
213+ PRINT_BUFFER_1 (frameData );
204214 printf (" \n " );
205215#endif /* ENABLE_JSON */
206216 } else {
207217 printf (" Empty frame\n " );
208218 }
209- } else if (frame-> getDataLength () > 0 ) {
219+ } else if (frameData. getLength () > 0 ) {
210220 printf (" \n " );
211- printIndented (4 , frame-> getData (), frame-> getDataLength ());
221+ printIndented (4 , frameData. getData (), frameData. getLength ());
212222 }
213223
214224 printf (" \n " );
@@ -229,8 +239,8 @@ bool isWebSocketPort(unsigned short port) {
229239}
230240
231241void handleTcpPacket (struct timeval tv, const RawIpPacket* ip, const RawTcpPacket* tcp) {
232- const char * tcpData = ((const char *) tcp) + tcp->th_off * 4 ;
233242 uint16_t tcpDataLen = ntohs (ip->ip_len ) - sizeof (RawIpPacket) - tcp->th_off * 4 ;
243+ Buffer buffer (((const char *) tcp) + tcp->th_off * 4 , tcpDataLen);
234244
235245 if (!isPacketAllowedByFilters (ip)) {
236246 return ;
@@ -249,14 +259,14 @@ void handleTcpPacket(struct timeval tv, const RawIpPacket* ip, const RawTcpPacke
249259
250260 if (isWebSocketPort (src.getPort ()) || isWebSocketPort (dest.getPort ())) {
251261 WebSocketParser* parser = isIncoming ? party->getWebSocketParserIncoming () : party->getWebSocketParserOutgoing ();
252- handleWebsocketNotification (partyName, isIncoming, tv, parser, tcpData, tcpDataLen );
262+ handleWebsocketNotification (partyName, isIncoming, tv, parser, buffer );
253263 } else if (isHttpPort (src.getPort ()) || isHttpPort (dest.getPort ())) {
254264 printPacketInfo (partyName, isIncoming, tv);
255265
256266 if (isIncoming) {
257- handleHttpResponse (tcpData, tcpDataLen );
267+ handleHttpResponse (buffer );
258268 } else {
259- handleHttpRequest (tcpData, tcpDataLen );
269+ handleHttpRequest (buffer );
260270 }
261271 }
262272
0 commit comments