]> granicus.if.org Git - curl/commitdiff
email: Moved server greeting responses into separate functions
authorSteve Holme <steve_holme@hotmail.com>
Sat, 24 Mar 2012 11:55:34 +0000 (11:55 +0000)
committerSteve Holme <steve_holme@hotmail.com>
Sat, 24 Mar 2012 11:55:34 +0000 (11:55 +0000)
Moved the server greeting response handling code from the statemach_act
functions to separate response functions. This makes the code simpler
to follow and provides consistency with the other responses that are
handled here.

lib/imap.c
lib/pop3.c
lib/smtp.c

index 2aeda785be7970165c4765e60546ba886ced04d8..42b76fe1bb44a8d2db5b2ca6c0b58693f6fff089 100644 (file)
@@ -343,6 +343,36 @@ static void imap_to_imaps(struct connectdata *conn)
 #define imap_to_imaps(x) Curl_nop_stmt
 #endif
 
+/* for the initial server greeting */
+static CURLcode imap_state_servergreet_resp(struct connectdata *conn,
+                                            int imapcode,
+                                            imapstate instate)
+{
+  CURLcode result = CURLE_OK;
+  struct SessionHandle *data = conn->data;
+
+  (void)instate; /* no use for this yet */
+
+  if(imapcode != 'O') {
+    failf(data, "Got unexpected imap-server response");
+    return CURLE_FTP_WEIRD_SERVER_REPLY;
+  }
+
+  if(data->set.use_ssl && !conn->ssl[FIRSTSOCKET].use) {
+    /* We don't have a SSL/TLS connection yet, but SSL is requested. Switch
+       to TLS connection now */
+    const char *str;
+
+    str = getcmdid(conn);
+    result = imapsendf(conn, str, "%s STARTTLS", str);
+    state(conn, IMAP_STARTTLS);
+  }
+  else
+    result = imap_state_login(conn);
+
+  return result;
+}
+
 /* for STARTTLS responses */
 static CURLcode imap_state_starttls_resp(struct connectdata *conn,
                                          int imapcode,
@@ -373,7 +403,9 @@ static CURLcode imap_state_starttls_resp(struct connectdata *conn,
       }
     }
   }
+
   state(conn, IMAP_STOP);
+
   return result;
 }
 
@@ -400,6 +432,7 @@ static CURLcode imap_state_login_resp(struct connectdata *conn,
 {
   CURLcode result = CURLE_OK;
   struct SessionHandle *data = conn->data;
+
   (void)instate; /* no use for this yet */
 
   if(imapcode != 'O') {
@@ -408,6 +441,7 @@ static CURLcode imap_state_login_resp(struct connectdata *conn,
   }
 
   state(conn, IMAP_STOP);
+
   return result;
 }
 
@@ -422,6 +456,7 @@ static CURLcode imap_state_fetch_resp(struct connectdata *conn,
   struct FTP *imap = data->state.proto.imap;
   struct pingpong *pp = &imapc->pp;
   const char *ptr = data->state.buffer;
+
   (void)instate; /* no use for this yet */
 
   if('*' != imapcode) {
@@ -489,6 +524,7 @@ static CURLcode imap_state_fetch_resp(struct connectdata *conn,
     result = CURLE_FTP_WEIRD_SERVER_REPLY; /* TODO: fix this code */
 
   state(conn, IMAP_STOP);
+
   return result;
 }
 
@@ -558,7 +594,6 @@ static CURLcode imap_statemach_act(struct connectdata *conn)
 {
   CURLcode result;
   curl_socket_t sock = conn->sock[FIRSTSOCKET];
-  struct SessionHandle *data=conn->data;
   int imapcode;
   struct imap_conn *imapc = &conn->proto.imapc;
   struct pingpong *pp = &imapc->pp;
@@ -580,24 +615,7 @@ static CURLcode imap_statemach_act(struct connectdata *conn)
   /* we have now received a full IMAP server response */
   switch(imapc->state) {
   case IMAP_SERVERGREET:
-    if(imapcode != 'O') {
-      failf(data, "Got unexpected imap-server response");
-      return CURLE_FTP_WEIRD_SERVER_REPLY;
-    }
-
-    if(data->set.use_ssl && !conn->ssl[FIRSTSOCKET].use) {
-      /* We don't have a SSL/TLS connection yet, but SSL is requested. Switch
-         to TLS connection now */
-      const char *str;
-
-      str = getcmdid(conn);
-      result = imapsendf(conn, str, "%s STARTTLS", str);
-      state(conn, IMAP_STARTTLS);
-    }
-    else
-      result = imap_state_login(conn);
-    if(result)
-      return result;
+    result = imap_state_servergreet_resp(conn, imapcode, imapc->state);
     break;
 
   case IMAP_LOGIN:
index c95f45e19693cd1f61c5eb4585a474fc29690323..6b7e01a608c2290db03831ebf8e1b5cd8046c143 100644 (file)
@@ -287,6 +287,34 @@ static void pop3_to_pop3s(struct connectdata *conn)
 #define pop3_to_pop3s(x) Curl_nop_stmt
 #endif
 
+/* for the initial server greeting */
+static CURLcode pop3_state_servergreet_resp(struct connectdata *conn,
+                                            int pop3code,
+                                            pop3state instate)
+{
+  CURLcode result = CURLE_OK;
+  struct SessionHandle *data = conn->data;
+  struct pop3_conn *pop3c = &conn->proto.pop3c;
+
+  (void)instate; /* no use for this yet */
+
+  if(pop3code != 'O') {
+    failf(data, "Got unexpected pop3-server response");
+    return CURLE_FTP_WEIRD_SERVER_REPLY;
+  }
+
+  if(data->set.use_ssl && !conn->ssl[FIRSTSOCKET].use) {
+    /* We don't have a SSL/TLS connection yet, but SSL is requested. Switch
+       to TLS connection now */
+    result = Curl_pp_sendf(&pop3c->pp, "STLS");
+    state(conn, POP3_STARTTLS);
+  }
+  else
+    result = pop3_state_user(conn);
+
+  return result;
+}
+
 /* for STARTTLS responses */
 static CURLcode pop3_state_starttls_resp(struct connectdata *conn,
                                          int pop3code,
@@ -294,6 +322,7 @@ static CURLcode pop3_state_starttls_resp(struct connectdata *conn,
 {
   CURLcode result = CURLE_OK;
   struct SessionHandle *data = conn->data;
+
   (void)instate; /* no use for this yet */
 
   if(pop3code != 'O') {
@@ -316,6 +345,7 @@ static CURLcode pop3_state_starttls_resp(struct connectdata *conn,
       state(conn, POP3_STOP);
     }
   }
+
   return result;
 }
 
@@ -342,6 +372,7 @@ static CURLcode pop3_state_user_resp(struct connectdata *conn,
     return result;
 
   state(conn, POP3_PASS);
+
   return result;
 }
 
@@ -360,6 +391,7 @@ static CURLcode pop3_state_pass_resp(struct connectdata *conn,
   }
 
   state(conn, POP3_STOP);
+
   return result;
 }
 
@@ -518,7 +550,6 @@ static CURLcode pop3_statemach_act(struct connectdata *conn)
 {
   CURLcode result;
   curl_socket_t sock = conn->sock[FIRSTSOCKET];
-  struct SessionHandle *data=conn->data;
   int pop3code;
   struct pop3_conn *pop3c = &conn->proto.pop3c;
   struct pingpong *pp = &pop3c->pp;
@@ -536,21 +567,7 @@ static CURLcode pop3_statemach_act(struct connectdata *conn)
     /* we have now received a full POP3 server response */
     switch(pop3c->state) {
     case POP3_SERVERGREET:
-      if(pop3code != 'O') {
-        failf(data, "Got unexpected pop3-server response");
-        return CURLE_FTP_WEIRD_SERVER_REPLY;
-      }
-
-      if(data->set.use_ssl && !conn->ssl[FIRSTSOCKET].use) {
-        /* We don't have a SSL/TLS connection yet, but SSL is requested. Switch
-           to TLS connection now */
-        result = Curl_pp_sendf(&pop3c->pp, "STLS");
-        state(conn, POP3_STARTTLS);
-      }
-      else
-        result = pop3_state_user(conn);
-      if(result)
-        return result;
+      result = pop3_state_servergreet_resp(conn, pop3code, pop3c->state);
       break;
 
     case POP3_USER:
@@ -585,6 +602,7 @@ static CURLcode pop3_statemach_act(struct connectdata *conn)
       break;
     }
   }
+
   return result;
 }
 
index 83edb42b826176737baa82bafd78e7abcc5d7e26..08fe86f24c7d7e44197ffc6b86bd3b9bd4068ac5 100644 (file)
@@ -499,6 +499,26 @@ static void smtp_to_smtps(struct connectdata *conn)
 #define smtp_to_smtps(x) Curl_nop_stmt
 #endif
 
+/* for the initial server greeting */
+static CURLcode smtp_state_servergreet_resp(struct connectdata *conn,
+                                            int smtpcode,
+                                            smtpstate instate)
+{
+  CURLcode result = CURLE_OK;
+  struct SessionHandle *data = conn->data;
+
+  (void)instate; /* no use for this yet */
+
+  if(smtpcode/100 != 2) {
+    failf(data, "Got unexpected smtp-server response: %d", smtpcode);
+    return CURLE_FTP_WEIRD_SERVER_REPLY;
+  }
+
+  result = smtp_state_ehlo(conn);
+
+  return result;
+}
+
 /* for STARTTLS responses */
 static CURLcode smtp_state_starttls_resp(struct connectdata *conn,
                                          int smtpcode,
@@ -506,6 +526,7 @@ static CURLcode smtp_state_starttls_resp(struct connectdata *conn,
 {
   CURLcode result = CURLE_OK;
   struct SessionHandle *data = conn->data;
+
   (void)instate; /* no use for this yet */
 
   if(smtpcode != 220) {
@@ -1066,14 +1087,15 @@ static CURLcode smtp_state_data_resp(struct connectdata *conn,
                       FIRSTSOCKET, smtp->bytecountp);
 
   state(conn, SMTP_STOP);
+
   return CURLE_OK;
 }
 
 /* for the POSTDATA response, which is received after the entire DATA
    part has been sent off to the server */
 static CURLcode smtp_state_postdata_resp(struct connectdata *conn,
-                                     int smtpcode,
-                                     smtpstate instate)
+                                         int smtpcode,
+                                         smtpstate instate)
 {
   CURLcode result = CURLE_OK;
 
@@ -1117,14 +1139,7 @@ static CURLcode smtp_statemach_act(struct connectdata *conn)
     /* we have now received a full SMTP server response */
     switch(smtpc->state) {
     case SMTP_SERVERGREET:
-      if(smtpcode/100 != 2) {
-        failf(data, "Got unexpected smtp-server response: %d", smtpcode);
-        return CURLE_FTP_WEIRD_SERVER_REPLY;
-      }
-
-      result = smtp_state_ehlo(conn);
-      if(result)
-        return result;
+      result = smtp_state_servergreet_resp(conn, smtpcode, smtpc->state);
       break;
 
     case SMTP_EHLO: