Преглед изворни кода

core: memset 0 the struct in first line parsing; split type and flags

- initialize to 0 first line struct msg_start_t in parse_first_line()
- split field int type in short type and short flags to be able to store
  more info about first line without changes in other places of existing code
- set in flags if the protocol in first line is sip or http - useful to
  avoid string comparison whenever needed to get the two very used
  protocols
Daniel-Constantin Mierla пре 10 година
родитељ
комит
9328918897
2 измењених фајлова са 36 додато и 2 уклоњено
  1. 34 1
      parser/parse_fline.c
  2. 2 1
      parser/parse_fline.h

+ 34 - 1
parser/parse_fline.c

@@ -45,6 +45,11 @@
 #include "../mem/mem.h"
 #include "../ut.h"
 
+/* flags for first line
+ * - stored on a short field (16 flags) */
+#define FLINE_FLAG_PROTO_SIP	(1<<0)
+#define FLINE_FLAG_PROTO_HTTP	(1<<1)
+
 int http_reply_parse = 0;
 
 /* grammar:
@@ -56,7 +61,7 @@ int http_reply_parse = 0;
 
 /* parses the first line, returns pointer to  next line  & fills fl;
    also  modifies buffer (to avoid extra copy ops) */
-char* parse_first_line(char* buffer, unsigned int len, struct msg_start * fl)
+char* parse_first_line(char* buffer, unsigned int len, struct msg_start* fl)
 {
 	
 	char *tmp;
@@ -77,6 +82,7 @@ char* parse_first_line(char* buffer, unsigned int len, struct msg_start * fl)
 	*/
 	
 
+	memset(fl, 0, sizeof(struct msg_start));
 	offset = 0;
 	end=buffer+len;
 	/* see if it's a reply (status) */
@@ -97,6 +103,7 @@ char* parse_first_line(char* buffer, unsigned int len, struct msg_start * fl)
 		strncasecmp( tmp+1, SIP_VERSION+1, SIP_VERSION_LEN-1)==0 &&
 		(*(tmp+SIP_VERSION_LEN)==' ')) {
 			fl->type=SIP_REPLY;
+			fl->flags|=FLINE_FLAG_PROTO_SIP;
 			fl->u.reply.version.len=SIP_VERSION_LEN;
 			tmp=buffer+SIP_VERSION_LEN;
 	} else if (http_reply_parse != 0 &&
@@ -111,6 +118,7 @@ char* parse_first_line(char* buffer, unsigned int len, struct msg_start * fl)
 			 *       - the message is marked as SIP_REPLY (ugly)
 			 */
 				fl->type=SIP_REPLY;
+				fl->flags|=FLINE_FLAG_PROTO_HTTP;
 				fl->u.reply.version.len=HTTP_VERSION_LEN+1 /*include last digit*/;
 				tmp=buffer+HTTP_VERSION_LEN+1 /* last digit */;
 	} else IFISMETHOD( INVITE, 'I' )
@@ -223,6 +231,22 @@ char* parse_first_line(char* buffer, unsigned int len, struct msg_start * fl)
 	fl->u.request.version.len=tmp-third;
 	fl->len=nl-buffer;
 
+	if (fl->type==SIP_REQUEST) {
+		if(fl->u.request.version.len >= SIP_VERSION_LEN
+				&& (fl->u.request.version.s[0]=='S'
+					|| fl->u.request.version.s[0]=='s')
+				&& !strncasecmp(fl->u.request.version.s+1,
+					SIP_VERSION+1, SIP_VERSION_LEN-1)) {
+			fl->flags|=FLINE_FLAG_PROTO_SIP;
+		} else if(fl->u.request.version.len >= HTTP_VERSION_LEN
+				&& (fl->u.request.version.s[0]=='H'
+					|| fl->u.request.version.s[0]=='h')
+				&& !strncasecmp(fl->u.request.version.s+1,
+					HTTP_VERSION+1, HTTP_VERSION_LEN-1)) {
+			fl->flags|=FLINE_FLAG_PROTO_HTTP;
+		}
+	}
+
 	return nl;
 
 error:
@@ -245,3 +269,12 @@ error1:
 	nl=eat_line(buffer,len);
 	return nl;
 }
+
+char* parse_fline(char* buffer, char* end, struct msg_start* fl)
+{
+	if(end<=buffer) {
+		/* make it throw error via parse_first_line() for consistency */
+		return parse_first_line(buffer, 0, fl);
+	}
+	return parse_first_line(buffer, (unsigned int)(end-buffer), fl);
+}

+ 2 - 1
parser/parse_fline.h

@@ -69,7 +69,8 @@
 #define PUBLISH_LEN 7
 
 struct msg_start {
-	int type;					/*!< Type of the Message - Request/Response */
+	short type;					/*!< Type of the message - request/response */
+	short flags;				/*!< First line flags */
 	int len; 					/*!< length including delimiter */
 	union {
 		struct {