hc-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Dmitry (JIRA)" <j...@apache.org>
Subject [jira] [Commented] (HTTPCORE-316) HeaderGroup clone removes headers from original
Date Thu, 07 Apr 2016 14:16:25 GMT

    [ https://issues.apache.org/jira/browse/HTTPCORE-316?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=15230301#comment-15230301
] 

Dmitry commented on HTTPCORE-316:
---------------------------------

{code}
    @Test
    public void test() throws Exception {
        HeaderGroup original = new HeaderGroup();
        HeaderGroup clone = (HeaderGroup) original.clone();

        original.addHeader(new BasicHeader("name", "1"));

        assertThat(clone.getHeaders("name").length, equalTo(0));
    }
{code}

fails with

{code}
java.lang.AssertionError: 
Expected: <0>
     but: was <1>
 <Click to see difference>
	at org.hamcrest.MatcherAssert.assertThat(MatcherAssert.java:20)
	at org.hamcrest.MatcherAssert.assertThat(MatcherAssert.java:8)
...
{code}

httpcommons 4.3.3

There is copy() method that does deep copy but I just cannot see a point in clone() that does
not do the same. If it was a design choice, would be interesting to know why.

So to me it is a bug but if you consider it a feature, then the bug is in httpclient that
is not aware of that feature and does not clone request properly.

Cheers.

> HeaderGroup clone removes headers from original
> -----------------------------------------------
>
>                 Key: HTTPCORE-316
>                 URL: https://issues.apache.org/jira/browse/HTTPCORE-316
>             Project: HttpComponents HttpCore
>          Issue Type: Bug
>          Components: HttpCore
>    Affects Versions: 4.2.2
>         Environment: All platforms
>            Reporter: Markus Thies
>              Labels: HeaderGroup, patch
>             Fix For: 4.2.3
>
>   Original Estimate: 2h
>  Remaining Estimate: 2h
>
> The class org.apache.http.message.HeaderGroup  provides a method clone().
> If clone is called the original object's headers are removed.
> The root cause of this is, that the class HeaderGroup has only one attribute (headers)
which is a List.
> As clone does a shallow copy the List is not cloned (that is correct).
> But within the method clone(), the headers of the newly created clone are removed (by
calling clear()) but they actually also clear the headers of the original object (since it
is not a copy).
> So this leads to very tricky problems in code where the headers are essential to be available
in the clone and in the original object.
> Original code:
>     public Object clone() throws CloneNotSupportedException {
>         HeaderGroup clone = (HeaderGroup) super.clone();
>         clone.headers.clear();
>         clone.headers.addAll(this.headers);
>         return clone;
>     }
> Corrected code:
>     public Object clone() throws CloneNotSupportedException {
>         HeaderGroup clone = (HeaderGroup) super.clone();
>         //BUG: would also clear the headers original 
>         //clone.headers.clear();
>         //clone.headers.addAll(this.headers);
>         return clone;
>     }



--
This message was sent by Atlassian JIRA
(v6.3.4#6332)

---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@hc.apache.org
For additional commands, e-mail: dev-help@hc.apache.org


Mime
View raw message