Receive http post put data

Hi all,
I’m trying to make a code to receive incoming data from a PLC.
This data can be a http PUT or http POST, but the problem comes when this data is sent to the arduino into a different TCP segments.
I know the content-length form the headers and now the problem that I have is that the arduino is waiting for that incoming message and does not stop never. Then the other incoming messages are not being accepted because the arduino is busy processing the first message!

I put my code:

#include <SPI.h>
#include <Ethernet.h>

// Enter a MAC address and IP address for your controller below.
// The IP address will be dependent on your local network:
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
IPAddress ip( 192,168,1,49 );
//IPAddress gateway( 192,168,1,1 );
//IPAddress subnet( 255,255,240,0 );
//IPAddress MyDns( 192,168,1,1 );

// Initialize the Ethernet server library
// with the IP address and port you want to use 
// (port 80 is default for HTTP):
EthernetServer server(80);

//DECLARACION DE VARIABLES
int counter=0;
String StrLastMssage;

/*
inHead[0] = method. Example: PUT, GET, POST...
inHead[1] = URI
inHead[2] = HTPP/1.1
*/



// CONFIGURATION
const int DELAY= 2;
const String VERSION="0.5 BETA";

void setup()
{
  Serial.begin(9600);

  // set SPI SS pins on w5100 and SD
  pinMode(10,OUTPUT);
  digitalWrite(10,HIGH);
  pinMode(4,OUTPUT);
  digitalWrite(4,HIGH);

  // start the SD interface here if you want.
  // Add the SD.h library above
  // SD.begin(4);

  // start the Ethernet connection and the server:
  Ethernet.begin(mac, ip);
  // disable w5100 SPI so SD SPI can work with it
  digitalWrite(10,HIGH);
  delay(2000);
  server.begin();

  Serial.println("setup finished");
  Serial.print("Software Version: ");
  Serial.println(VERSION);
  Serial.print("IP Address: ");
  Serial.println(ip);
  Serial.println();
  
}

void loop() {

 Listen();
 
}

int Method_Number(String method) {
 int result=0;
 /*
 Serial.print("Debug: Method_Number function in string received: ");
 Serial.println(method);
 */
 

 
 if (method=="PUT"||method=="POST"){
   result=1;
 }
 if (method=="GET"){
   result=2;
 }
  
return result;
}



void Listen() {
String Headers="", inHead[3] ="", ContentLength[2], inData="";
int level=0,DataLength=0,lastNum=0;
boolean CharExceeded=false, ContentLengthOK=false, debug=true, currentLineIsBlank = true,ContentLengthValueOK=false; 
// listen for incoming clients
  EthernetClient client = server.available();
  if (client) {
    if  (debug==true){
      Serial.println("**************************************************************************");  
    }
    
    Serial.println("Incoming message...");
    // an http request ends with a blank line
    while (client.connected()) {
      while(client.available()) {
       char c = client.read();
       Headers += (char)c;
       if(c!='\r'&&CharExceeded==false&&c!=' '){
          inHead[level] += (char)c; 
       } else if (CharExceeded==true&&(inHead[0]=="PUT"||inHead[0]=="POST")&&c!='\r'&&c!=' '&&c!='\n'&&ContentLengthValueOK==false) {
            ContentLength[level] += (char)c;

       }else{

         switch (c){
          case '\r':             
              if (CharExceeded==true&&(inHead[0]=="PUT"||inHead[0]=="POST")&&ContentLengthOK==true) {
                ContentLengthValueOK=true;
              }else {
                ContentLength[0]="";
              }
              CharExceeded=true;
              level=0;
            break;
          case ' ':
            if (CharExceeded==true&&(inHead[0]=="PUT"||inHead[0]=="POST")&&ContentLengthOK==false) {
              if (ContentLength[0]=="Content-Length:"){
                level ++;
                ContentLengthOK=true;
              }else{
                 ContentLength[0]="";
                 level=0;
              }
            }else if(CharExceeded==false){
              level ++;
            }
            break;

         }


         
       }
       //Serial.print(c);
     
        // if you've gotten to the end of the line (received a newline
        // character) and the line is blank, the http request has ended,
        // so you can send a reply
        if (c == '\n' && currentLineIsBlank) {

// Respuesta
  if (debug==true){
   Serial.print("Method number: ");
   Serial.println(Method_Number(inHead[0]));
   Serial.print("Content-Length: ");
   Serial.println(ContentLength[1]); 
  }


      switch (Method_Number(inHead[0])){ 
case 1:
        DataLength=ContentLength[1].toInt();
        //while(inData.length()<=DataLength){

              inData += (char)client.read();
              lastNum=inData.length();
                //char cc = client.read();
                //Serial.print(cc);
                
               //Serial.print(inData);
               if (inData.length()>DataLength){
                  StrLastMssage=inData;
                  Serial.println("Message received");
                  Serial.println("Sending response");
                  // send a standard http response header
                  client.println("HTTP/1.1 204 No Content");
                  client.print("Server: Blackoffice ");
                  client.println(VERSION);
                  client.println("Content-length: 0");
                  client.println();
                  client.stop();
                }

                
               delay (2);
              
        
       
        Serial.println(client.connected());
        

break;

case 2:
GET_RESPONSE(client);                    

break;


default:
NO_RESPONSE(client);
break;
        }
        }
        else if (c == '\n') {
          // you're starting a new line
          currentLineIsBlank = true;
        //        Serial.print(":true ");
        } 
        else if (c != '\r') {
          // you've gotten a character on the current line
          currentLineIsBlank = false;
        
      //Serial.print(":false ");
      }
      

 //delay (DELAY); 
}
    }
    counter++;
    Serial.println("Disconnected");
    Serial.println();

    

    if (debug==true){
      //Serial.print("Headers:");
      //Serial.println(Headers);
      Serial.print("Content-Length: ");
      Serial.println(ContentLength[1]);
      Serial.print("Data: ");
    }
    Serial.println(inData);

    if (debug==true){
      Serial.print("Method: ");
      Serial.println(inHead[0]);
      Serial.print("URI: ");
      Serial.println(inHead[1]);
      Serial.print("Info: ");
      Serial.println(inHead[2]);
      Serial.print("Event number: ");
      Serial.println(counter);
      Serial.println("**************************************************************************");
      Serial.println();

  
    }
    
  }

  
}



void GET_RESPONSE(EthernetClient cliente){
                    cliente.println("HTTP/1.1 200 OK");
                    cliente.println("Content-Type: text/html");
                    cliente.println("Connection: close");
                    cliente.println();
                    // send web page
                    cliente.println("<!DOCTYPE html>");
                    cliente.println("<html>");
                    cliente.println("<head>");
                    cliente.println("<title>Arduino Blackoffice</title>");
                    cliente.println("</head>");
                    cliente.println("<body>");
                    cliente.println("<h1>Blackoffice</h1>");
                    cliente.print("<p>Firmware version: ");
                    cliente.print(VERSION);
                    cliente.println("</p>");
                    cliente.println("<p>Last event:");
                    cliente.print(StrLastMssage);
                    cliente.println("</p>");
                    //cliente.println("<p>A web page from the Arduino server</p>");
                    cliente.println("</body>");
                    cliente.println("</html>");
                    cliente.stop();
  
}

void NO_RESPONSE (EthernetClient cliente){
  
          cliente.println("HTTP/1.1 501 Not Implemented");
          cliente.print("Server: Arduino ");
          cliente.println(VERSION);
          cliente.println();
          cliente.stop();
  
}

Can anyone help me with that?

Thanks.

Have you tried the example server/post code to see if it will work in your setup?

String Headers="", inHead[3] ="", ContentLength[2], inData="";

You declare an array of 3 Strings, and provide one initializer. What do you think will be in the other two instance?

    if  (debug==true){
      Serial.println("**************************************************************************"); 
    }

You really do seem to like pissing away resources uselessly. String instances galore, when strings would be far more appropriate, and long literals that convey no useful information.

       } else if (CharExceeded==true&&(inHead[0]=="PUT"||inHead[0]=="POST")&&c!='\r'&&c!=' '&&c!='\n'&&ContentLengthValueOK==false) {

Isyourspacekeybroken?Getitfixed.

if you want all the data in one chunk, the client must use http 1.0 (not 1.1) for PUT method.